How to use precompiled headers with a qmake extra compiler? - qmake

When using the default qmake compiler (via the SOURCES variable), I can use precompiled headers like so:
CONFIG += precompile_header
PRECOMPILED_HEADER = stable.h
SOURCES = main.c
However, I'd like to use a custom compiler (via QMAKE_EXTRA_COMPILERS). I tried this:
CONFIG += precompile_header
PRECOMPILED_HEADER = stable.h
MY_SOURCES = main.c
my.input = MY_SOURCES
my.output = ${QMAKE_FILE_IN_BASE}.o
my.commands = clang $$QMAKE_CFLAGS_USE_PRECOMPILE -c ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}
QMAKE_EXTRA_COMPILERS += my
...and the precompiled headers are built, but my custom compiler fails because QMAKE_CFLAGS_USE_PRECOMPILE doesn't contain the path to the precompiled header. (It is defined as -Xclang -include-pch -Xclang ${QMAKE_PCH_OUTPUT}, and apparently ${QMAKE_PCH_OUTPUT} is empty.)
How can I get the name of the generated precompiled header, so I can pass it as a parameter to my custom compiler?

Looking at the qmake source code, precompiled header handling seems to be hardcoded in UnixMakefileGenerator::init() to only work for the built-in C, CXX, OBJC, and OBJCXX compilers.
QMAKE_PCH_OUTPUT, for GCC and Clang-style precompiled headers, is constructed by combining PRECOMPILED_DIR, TARGET, one of c/c++/objective-c/objective-c++, and QMAKE_PCH_OUTPUT_EXT. So, in the question's second example, the following command line should work:
my.commands = clang -Xclang -include-pch -Xclang $$TARGET/c$$QMAKE_PCH_OUTPUT_EXT -c ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}

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.

what are the rules zig build-lib uses for shared objects filenames?

I have a sqlite extension file. The source is sqliteext/csv.c. When I build the lib with clang the output file is created at lib/csv.so.
cc -g -fPIC -I /usr/local/include -shared sqliteext/csv.c -o lib/csv.so
When I compile the lib using zig...
zig build-lib -I /usr/local/include -I /usr/include -I /usr/include/x86_64-linux-gnu --c-source sqliteext/csv.c -dynamic --output-dir lib
There are two problems.
zig prefixes the filename with a lib
zig add a version number thing in the suffix
so the output file is lib/libcsv.so.0.0.0
And what's interesting about this is that I need to change the filename in my extension loader (that's ok) but that I also need a symlink to handle the 0.0.0.
I'm still looking at the CLI help but I'm still not seeing the thing I need.
See https://github.com/ziglang/zig/issues/2230 and https://github.com/ziglang/zig/issues/2231
The former was fixed the same day as your post via https://github.com/ziglang/zig/pull/6315

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