How to build cockroach DB with customized CFLAGS, CXXFLAGS and LDFLAGS? - cockroachdb

I would like to build cockroach DB with CFLAGS += -O, CXXFLAGS += -O and
LDFLAGS ?= -static. How would I do that?

There are a couple of ways to do that.
(1) If you are building with make. You can simple open the make file and just find and override these flags.
(2) If you are building with bazel. You can set these flags with these bazel command options: --cxxopt=-O, --copt=-O, --linkopt=-static. For example: bazel build pkg/cmd/cockroach-oss --cxxopt=-O --copt=-O --linkopt=-static
(3) If you are building with crdb's dev tool (a wrapper of bazel). You can pass in the same options as (2) but following an extra --. For example, dev build oss -- --cxxopt=-O --copt=-O --linkopt=-static

Related

Where are set cmake compilation option for C_FLAGS after executing?

I have a c/cpp project.
I was previously using a single-configuration build (with only release configuration) and I have started to add a multi-configuration build with the configurations Release and Debug.
I want to be able to do make DEBUG=1 all in my project with compilator options:
-DDEBUG -ggdb3 -O0
So I have use this in my CMake configure command:
-DCMAKE_C_FLAGS_DEBUG="-DDEBUG -ggdb3 -O0"
-DCMAKE_CXX_FLAGS_DEBUG="-DDEBUG -ggdb3 -O0"
-DCMAKE_BUILD_TYPE="Debug"
When I type the CMake command to generate the makefiles, my options are not added in the Makefile for my project. I am not able to find "-DDEBUG -ggdb3 -O0" in cmake generated files.
Where does CMake add configuration flags option?
For your DEBUG=1 macro definition, you need to use target_compile_definitions and the $<CONFIG:cfgs> generator expression like so:
target_compile_definitions(my_target PUBLIC "$<$<CONFIG:Debug>:DEBUG=1>")
For your others, -DCMAKE_CXX_FLAGS_DEBUG="-ggdb3 -O0" on the commandline should work, and is not a bad way of doing it. You could also use list(APPEND) and a generator expression or if() block to only add it for the relevant compiler.

How to add CFLAGS and CXXFLAGS and LDFLAGS to file.cmake? [duplicate]

I am using the arm-linux-androideabi-g++ compiler. When I try to compile a simple "Hello, World!" program it compiles fine. When I test it by adding a simple exception handling in that code it works too (after adding -fexceptions .. I guess it is disabled by default).
This is for an Android device, and I only want to use CMake, not ndk-build.
For example - first.cpp
#include <iostream>
using namespace std;
int main()
{
try
{
}
catch (...)
{
}
return 0;
}
./arm-linux-androideadi-g++ -o first-test first.cpp -fexceptions
It works with no problem...
The problem ... I am trying to compile the file with a CMake file.
I want to add the -fexceptions as a flag. I tried with
set (CMAKE_EXE_LINKER_FLAGS -fexceptions ) or set (CMAKE_EXE_LINKER_FLAGS "fexceptions" )
and
set ( CMAKE_C_FLAGS "fexceptions")
It still displays an error.
Note: Given CMake evolution since this was answer was written in 2012, most of the suggestions here are now outdated/deprecated and have better alternatives.
Suppose you want to add those flags (better to declare them in a constant):
SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
SET(GCC_COVERAGE_LINK_FLAGS "-lgcov")
There are several ways to add them:
The easiest one (not clean, but easy and convenient, and works only for compile flags, C & C++ at once):
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
Appending to corresponding CMake variables:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
Using target properties, cf. doc CMake compile flag target property and need to know the target name.
get_target_property(TEMP ${THE_TARGET} COMPILE_FLAGS)
if(TEMP STREQUAL "TEMP-NOTFOUND")
SET(TEMP "") # Set to empty string
else()
SET(TEMP "${TEMP} ") # A space to cleanly separate from existing content
endif()
# Append our values
SET(TEMP "${TEMP}${GCC_COVERAGE_COMPILE_FLAGS}" )
set_target_properties(${THE_TARGET} PROPERTIES COMPILE_FLAGS ${TEMP} )
Right now I use method 2.
In newer versions of CMake you can set compiler and linker flags for a single target with target_compile_options and target_link_libraries respectively (yes, the latter sets linker options too):
target_compile_options(first-test PRIVATE -fexceptions)
The advantage of this method is that you can control propagation of options to other targets that depend on this one via PUBLIC and PRIVATE.
As of CMake 3.13 you can also use target_link_options to add linker options which makes the intent more clear.
Try setting the variable CMAKE_CXX_FLAGS instead of CMAKE_C_FLAGS:
set (CMAKE_CXX_FLAGS "-fexceptions")
The variable CMAKE_C_FLAGS only affects the C compiler, but you are compiling C++ code.
Adding the flag to CMAKE_EXE_LINKER_FLAGS is redundant.
The preferred way to specify toolchain-specific options is using CMake's toolchain facility. This ensures that there is a clean division between:
instructions on how to organise source files into targets -- expressed in CMakeLists.txt files, entirely toolchain-agnostic; and
details of how certain toolchains should be configured -- separated into CMake script files, extensible by future users of your project, scalable.
Ideally, there should be no compiler/linker flags in your CMakeLists.txt files -- even within if/endif blocks. And your program should build for the native platform with the default toolchain (e.g. GCC on GNU/Linux or MSVC on Windows) without any additional flags.
Steps to add a toolchain:
Create a file, e.g. arm-linux-androideadi-gcc.cmake with global toolchain settings:
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
set(CMAKE_CXX_FLAGS_INIT "-fexceptions")
(You can find an example Linux cross-compiling toolchain file here.)
When you want to generate a build system with this toolchain, specify the CMAKE_TOOLCHAIN_FILE parameter on the command line:
mkdir android-arm-build && cd android-arm-build
cmake -DCMAKE_TOOLCHAIN_FILE=$(pwd)/../arm-linux-androideadi-gcc.cmake ..
(Note: you cannot use a relative path.)
Build as normal:
cmake --build .
Toolchain files make cross-compilation easier, but they have other uses:
Hardened diagnostics for your unit tests.
set(CMAKE_CXX_FLAGS_INIT "-Werror -Wall -Wextra -Wpedantic")
Tricky-to-configure development tools.
# toolchain file for use with gcov
set(CMAKE_CXX_FLAGS_INIT "--coverage -fno-exceptions -g")
Enhanced safety checks.
# toolchain file for use with gdb
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-fsanitize=address,undefined -fsanitize-undefined-trap-on-error")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-fsanitize=address,undefined -static-libasan")
You can also add linker flags to a specific target using the LINK_FLAGS property:
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " ${flag}")
If you want to propagate this change to other targets, you can create a dummy target to link to.
This worked for me when I needed a precompile definition named "NO_DEBUG":
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -DNO_DEBUG")
Then from code
#ifdef NO_DEBUG
.....
With CMake 3.4+, APPEND can be used with the string command to add flags.
string(APPEND CMAKE_EXE_LINKER_FLAGS " -fexceptions")

ninja appends its default installation path to my DESTDIR [duplicate]

I'm using to building code with CMake; but I'm now faced with using meson to build a certain repository. With CMake and make, if I use something like:
cmake -DCMAKE_INSTALL_PREFIX=/some/where` build_dir
make -C build_dir
make -C build_dir install
then instead of the files going under /usr/local by default, they will go under /some/where, e.g. /some/where/bin for executables, /some/where/lib, for libraries etc.
What's the meson equivalent of doing this?
Inspired by: What is CMake equivalent of 'configure --prefix=DIR && make all install '?
It's actually similar to the autotools convention in terms of the switch:
meson setup --prefix=/path/of/installation/destination build_dir
ninja -C build_dir
ninja -C build_dir install
(you could drop the first ninja command; ninja install will build before it installs.)

Where to put makefile flags in a Xcode project?

In the makefile:
CUSTOM_CFLAGS=-Wall -ggdb3 -O3 -std=gnu99 -frename-registers -pthread -Wsign-compare -D_GNU_SOURCE
SYS_CFLAGS=-DNO_THREAD_LOCAL
LDFLAGS=-pthread -rdynamic
LIBS=-lm -ldl
Where should I put the above in Xcode project?
When building a Makefile-based project with Xcode using the "External Build System" template
…you can add any necessary environment variables such as CFLAGS or LDFLAGS to the build settings for the project, as shown below:
These values are then exported as environment variables during the build process, which make and the Makefile will pick up on, assuming your Makefile has, for example, something like this inside it:
CXXFLAGS=$(CFLAGS) $(LDFLAGS) -std=c++11
g++ $(CXXFLAGS) main.cpp
This is what I needed to do to build a Makefile-based project that depended on the GNU Scientific Library, using the gsl package from Macports, which puts everything in /opt/local instead of the "standard" places where Xcode looks for shared libraries and headers.
Maybe this can help somebody else in this situation.
Makefile flags correspond to Xcode build settings. Look into the following build settings:
Optimization Level for the -O3 flag.
C Language Dialect for the -std=gnu99 flag.
Other C Flags for the other flags in the CUSTOM_CFLAGS and SYS_CFLAGS flags.
Other Linker Flags for the LDFLAGS and LIBS flags.
Xcode may have build settings for some of the flags in the makefile's CUSTOM_CFLAGS flag list and the linker flags. I haven't memorized the build settings list. If you open the Quick Help inspector, you can read a description of each build setting by selecting the build setting from the build settings list. Choose View > Utilities > Show Quick Help Inspector to show the Quick Help inspector.
If you don't know where to find Xcode's build settings, read the following article:
Xcode 4: Accessing Build Settings

Using GNU Make to build both debug and release targets at the same time

I'm working on a medium sized project which contains several libraries with interdependence's which I've recently converted over to build using a non-recursive makefile. My next goal is to enable building of both debug and release builds out of the same source tree at the same time (make debug;make release). My first step was to make debug and release targets which contained the correct build flags. I did this using target specific variables, like this:
CXXFLAGS=-Wall -Wextra -Werror -DLINUX
CXX_DEBUG_FLAGS=-g3 -DDEBUG_ALL
CXX_RELEASE_FLAGS=-O3
.PHONY: debug
debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS)
debug: build
.PHONY: release
release: CXXFLAGS+=$(CXX_RELEASE_FLAGS)
release: build
This worked fine, but you could only build debug, or release, not both at the same time. And by same time, I don't mean during the same build I mean back to back in the same source tree (make debug;make release). In order to do this I need to place the object files in a debug/release specific directory so they don't overwrite each other and I need to mangle the debug target binary name with a 'D'. I though this would be easy as I could just use target specific variables again, like this:
CXXFLAGS=-Wall -Wextra -Werror -DLINUX
CXX_DEBUG_FLAGS=-g3 -DDEBUG_ALL
CXX_RELEASE_FLAGS=-O3
.PHONY: debug
debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS)
debug: MODULE_BLD_TYPE=D
debug: OUT_DIR=debug_obj
debug: build
.PHONY: release
release: CXXFLAGS+=$(CXX_RELEASE_FLAGS)
release: MODULE_BLD_TYPE:=
release: OUT_DIR=release_obj
release: build
.PHONY: build
build: TARGET_NAME=HelloWorld$(MODULE_BLD_TYPE)
build: TARGET_BUILD_DIR=$(PROJECT_ROOT_DIR)/$(OUT_DIR)
build: TARGET_BUILD_OBJS=$(addprefix $(TARGET_BUILD_DIR)/,$(SOURCES:.cpp=.o))
build: $(TARGET_NAME)
You make experts reading this already know this won't work because you can't use target specific variables to create actual targets. They worked fine for my CXXFLAGS var because the variable wasn't used in a target name.
Is there a design pattern and or best practice to managing debug/release builds using non-recursive makefiles? Specificly, how do I build the object file directory path and target name (build a target based on a target)?
One of the most persistent problems with Make is its inability to handle more than one wildcard at a time. There is no really clean way to do what you ask (without resorting to recursion, which I don't think is really so bad). Here is a reasonable approach:
CXXFLAGS=-Wall -Wextra -Werror -DLINUX
CXX_DEBUG_FLAGS=-g3 -DDEBUG_ALL
CXX_RELEASE_FLAGS=-O3
.PHONY: debug
debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS)
debug: HelloWorldD
.PHONY: release
release: CXXFLAGS+=$(CXX_RELEASE_FLAGS)
release: HelloWorld
DEBUG_OBJECTS = $(addprefix $(PROJECT_ROOT_DIR)/debug_obj/,$(SOURCES:.cpp=.o))
RELEASE_OBJECTS = $(addprefix $(PROJECT_ROOT_DIR)/release_obj/,$(SOURCES:.cpp=.o))
HelloWorldD: $(DEBUG_OBJECTS)
HelloWorld: $(RELEASE_OBJECTS)
# And let's add three lines just to ensure that the flags will be correct in case
# someone tries to make an object without going through "debug" or "release":
CXX_BASE_FLAGS=-Wall -Wextra -Werror -DLINUX
$(DEBUG_OBJECTS): CXXFLAGS=$(CXX_BASE_FLAGS) $(CXX_DEBUG_FLAGS)
$(RELEASE_OBJECTS): CXXFLAGS=$(CXX_BASE_FLAGS) $(CXX_RELEASE_FLAGS)
Use VPATH to make debug and release builds use the same set of source files. The debug and release build can have their own directory, which means they'll have their object files separated.
Alternatively, use a build tool that supports out-of-source builds natively, like automake or (ugh) cmake.
If you enable the automake option subdir-objects (as in AM_INIT_AUTOMAKE([foreign subdir-objects])), you can write a non-recursive Makefile.am.

Resources