Output Include Paths and CMake - path

I would like to force CMake to provide verbosity in terms of include paths (-I...). Consider current output:
/D/Software/MinGW/bin/g++.exe -DQT_DEBUG -fmessage-length=0 -mfpmath=sse -msse2 -fopenmp -g3 -gdwarf-2 #CMakeFiles/go.dir/includes_CXX.rsp -o CMakeFiles/go.dir/main.cpp.obj -c /D/Users/Haroogan/Development/Workspace/New2/main.cpp
Everywhere, instead of showing a list of include paths, it rather shows #CMakeFiles/go.dir/includes_CXX.rsp. Indeed, this file contains include paths.
How can I force CMake or Make or whatever to output include paths?

set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 0)

When using Unix Makefiles (which it looks like you are) you can get the full set of command line flags by typing,
make VERBOSE=1
I think some of the other generators also have a cache variable that can be set.

The parameter #CMakeFiles/go.dir/includes_CXX.rsp is in fact what is passed to the compiler, which then reads this file and interprets it as a list of options. See man gcc for description of this mechanism.
The mechanism is used because shells on MS Windows have a limit to the length of the command line which can be easily exceeded in complex compilation calls. Therefore it is much safer to use files to pass parameters

Related

LLVM - Run concrete pass with clang [duplicate]

I am working on LLVM obfuscation project. I have written a llvm pass(lets say flow flattening pass) which i am running on source (test.c) with following command:
clang -emit-llvm test.c -c -o test.bc
opt -load ../../.. LLVMFlattening.so -fla <test.bc>/dev/null
But i have seen that in O-LLVM project they achieved same thing using:
clang -emit-llvm test.c -c -o test.bc -mllvm -fla
Can someone tell me what is -mllvm here and how this changed to a simple command?
-mllvm means Additional arguments to forward to LLVM's option processing. Therefore -mllvm -fla will pass -fla to the LLVM's option processing.
Clang and LLVM could run seperately. If you want clang to run llvm, and also have some options which you want llvm to aware. -mllvm is what you need.
Defautly, LLVM does not turn on all the transformation passes. With -fla, LLVM will turn on the pass registered with command line argument fla by call function RegisterPass<typename passName>.
In your command line, opt's -load option is used to load plugin. If you want to use the simple command line as expect. Your pass need to be linked into the opt binary. This could be done in the following two ways:
(Without modify the existing LLVM source tree): Add your only pass's source by adding CMakeLists.txt mentioned in this link
Directly copy your pass source code folder into <LLVM root>/lib/Transform directory. And modify the <LLVM root>/lib/Transform/CMakeLists.txt, add add_subdirectory(<pass name>) line just like others.
I'm working on O-LLVM rencently, and came into the same problem. Here is my solution:
1.add static cl::opt<bool> YOUR_FLA("fla", cl::init(false),"info...") to PassManagerBuilder.cpp
2.add function Pass *createYOUR_FLA(bool flag) in your obfuscation pass source code
3.add MPM.add(createYOUR_FLA(YOUR_FLA)); to function populateModulePassManager in PassManagerBuilder.cpp
The solution above works with my simple pass.

ClangCodeModel in QtCreator says "module not found" on imported C++20 modules

I use QtCreator 4.15.2, clang 12.0.1.
I have "generic project" with makefile. File src/test/main.cpp contains import test.StringUtil;. QtCreator complains that module is not found. (Project builds ok, but makefile uses gcc; I'm not familiar with clang.) I tried this:
Generated .pcm file in separate directory:
clang++ -std=c++20 -fmodules --precompile -x c++-module src/test/StringUtil.cpp -o target/clang/test.StringUtil.pcm
Edited myproject.cxxflags in project root so it contains:
-std=c++20 -fmodules -fprebuilt-module-path=/home/me/myproject/target/clang -Wall -Wextra -Wpedantic -fno-rtti
If I specify relative path -fprebuilt-module-path=target/clang then nothing changes; QtCreator still says that module not found. But with absolute path looks like ClangCodeModel gets disabled (crashes?): <No Symbols> in QtCreator's symbols combobox, no syntax errors reporting.
I also tried to compile in additiion to generating .pcm file, with no difference:
clang++ -std=c++20 -fmodules -c -x c++-module src/test/StringUtil.cpp -o target/build/clang/test.StringUtil.o
So: how do I make it work?
When I added support for modules in my own build system I found it easiest to specify each imported module with the flag -fmodule-file="path/to/other.pcm".
In the future I guess that the big build systems will do this aswell. since you need to preprocess the files anyway there is no extra cost. (It could be some extra work if you write your makefiles by hand though)
I don't think that qtcreators code model supports modules as of yet. I have tried but it does not work for me.

Is it possible to define CXXFLAGS with respect to the current compiler in bazel?

I'm trying to convert a Make project to Bazel. Currently, makefile detects the compiler being used and sets certain CXXFLAGS accordingly (not all flags are understood by all compilers).
For example:
ifeq ($(shell $(CC) -dM -E -x c++ /dev/null | grep __clang__),)
CXXFLAGS=-DUSE_GCC
else
CXXFLAGS=-DUSE_CLANG
endif
(Similarly code for compiler versions.)
How can I achieve this in bazel? The relation between a compiler and the flags to be used should be defined somewhere within the project (not on the commandline).
I know the --define option, but is there a way to directly relate the configration to the compiler being used?
Using the copts argument in your cc_library or cc_binary will set compiler flags.
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_binary_args

How to add directories to ld search path for a cross-compilation to ARM?

I am trying to configure util-linux to cross compile using arm-none-linux-gnueabi from CodeSourcery. My only problem so far is that it can't find my ncurses library which I compiled.
How can I add a directory to the ld search path? I've tried adding to my LIBRARY_PATH and LD_LIBRARY_PATH variables, but neither does anything. I know that I can add the -L flag to gcc and it will add to the linker path, but is there any way to do this globally, so that I can do it once, and not have to worry about it again?
Here is the output of arm-none-linux-gnueabi-gcc -print-search-dirs | grep libraries | sed 's/:/\n/g':
libraries
=/tools/bin/../lib/gcc/arm-none-linux-gnueabi/4.6.1/
/tools/bin/../lib/gcc/
/tools/bin/../lib/gcc/arm-none-linux-gnueabi/4.6.1/../../../../arm-none-linux-gnueabi/lib/arm-none-linux-gnueabi/4.6.1/
/tools/bin/../lib/gcc/arm-none-linux-gnueabi/4.6.1/../../../../arm-none-linux-gnueabi/lib/
/tools/bin/../arm-none-linux-gnueabi/libc/lib/arm-none-linux-gnueabi/4.6.1/
/tools/bin/../arm-none-linux-gnueabi/libc/lib/
/tools/bin/../arm-none-linux-gnueabi/libc/usr/lib/arm-none-linux-gnueabi/4.6.1/
/tools/bin/../arm-none-linux-gnueabi/libc/usr/lib/
I would like to add /arm/usr/lib and /arm/usr/local/lib to my ld search path.
If you need output from any other commands, just ask!
EDIT: I just found out about the CFLAGS environment variable--do all configure scripts/makefiles honor it?
Thank you!
If the ncurses library you compiled are going to be linked to the ARM binary you are cross-compiling you can not use LD_LIBRARY_PATH! LD_LIBRARY_PATH is only used by the current run-time and is in no way used by the compiler or linker when building your application.
The use of CFLAGS depends on creator of Makefile. CFLAGS are not automatically used even if they are defined as an environment variable. Only tools like the autoconf tools can pick them up from the environment and use them automagically. In the Makefiles find something like:
$(CC) $(CFLAGS) ....
if this fragment exists then the Makefile uses the CFLAGS variable. LDFLAGS is the more appropriate environment variable to use for link-time options.

How do I include a path to libraries in g++

I am trying to include the path to extra libraries in my makefile, but I can't figure out how to get the compiler to use that path. so far I have:
g++ -g -Wall testing.cpp fileparameters.cpp main.cpp -o test
and I want to include the path to
/data[...]/lib
because testing.cpp includes files from that library. Also, I'm on a linux machine.
EDIT: Not a path to a library. Just to files that were included. My bad.
To specify a directory to search for (binary) libraries, you just use -L:
-L/data[...]/lib
To specify the actual library name, you use -l:
-lfoo # (links libfoo.a or libfoo.so)
To specify a directory to search for include files (different from libraries!) you use -I:
-I/data[...]/lib
So I think what you want is something like
g++ -g -Wall -I/data[...]/lib testing.cpp fileparameters.cpp main.cpp -o test
These compiler flags (amongst others) can also be found at the GNU GCC Command Options manual:
3.16 Options for Directory Search
In your MakeFile or CMakeLists.txt you can set CMAKE_CXX_FLAGS as below:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/path/to/your/folder")
Alternatively you could setup environment variables.
Suppose you are using bash, then in ~/.bashrc, write
C_INCLUDE_PATH="/data/.../lib/:$C_INCLUDE_PATH" ## for C compiler
CPLUS_INCLUDE_PATH="/data/.../lib/:$CPLUS_INCLUDE_PATH" ## for Cpp compiler
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH
and source it with source ~/.bashrc.
You should be good to go.

Resources