How to generate preprocess and assmeble code by cmake? - c++

I am trying to get intermediate .i .s file by CMake when compiling .cpp file, but cmake default only output .o file. Is there any command to manipulate cmake to keep these intermediate file, thanks a lot!

If you are using gcc, try adding this line.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -save-temps=obj")

Which flag to use depends on the compiler you are using. Also, you should strongly prefer to inject such compiler-and-scenario-specific flags into the build externally, rather than set()-ing them inside the build.
For g++ or clang++, the following invocation would be appropriate:
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-save-temps=obj"
For MSVC it would be:
> cmake -S . -B build "-DCMAKE_CXX_FLAGS=/FA"

Related

cmake generate assembly file then compile it into executable

I'm using cmake, and let's say I have a .cpp file.
Is it possible in cmake to compile this cpp file into assembly code first, then do some other operation and after that create executable from the generated assembly?
Update:
So basically I want to compile Hello.cpp to Hello.s (later modify it, and then create the object file). However I can't generate the asm file with that code (It just creates a Hello.a static library):
add_library(Tutorial Hello.cpp)
set_target_properties(Tutorial PROPERTIES COMPILE_FLAGS "-S")
install (TARGETS Tutorial DESTINATION bin)
CMake does always generate "assembly file rules" for you. So let's say you have:
file(WRITE main.cpp "int main() { return 0; }")
add_executable(MyMain main.cpp)
You can call make for targets main.s or main.cpp.s like:
> make main.cpp.s
Compiling CXX source to assembly CMakeFiles/MyMain.dir/main.cpp.s
But if you want to automate everything, see my answer at your other question:
Call shell command after asm file generation in cmake
You can tell GCC to only generate assembly with the -S option.
You can also tell GCC to keep intermediate files with -save-temps.

How to configure autoreconf to use a different compiler than GCC

I am trying to compile the code for one of the projects and the source file uses autoreconf for generating the makefiles.
" autoreconf --verbose --force --make "
The problem is that this somehow generates the makefiles that uses the compiler as GCC. I want it to generate the configuration files with a different compiler. How can I do it?
I can issue the command as make CC= but this throws an error in the libtool that is used later on.
Thank You
Typically autoreconf just regenerates the configure script and the autoconf makefile templates, it doesn't create any actual makefiles.
Makefiles get created when configure is run, and that's when you want to override the compiler:
configure CC=clang CXX=clang++
that should create makefiles that use CC=clang and CXX=clang++

How to build coreutils with LLVM 3.4

I am trying to build GNU Coreutils 8.23 using the LLVM 3.4 tool-chain. One very important aspect is that I also need the LLVM bytecode for all the coreutils. Therefore, I need to include -emit-llvm in the CFLAGS. Therefore, I removed the $(CFLAGS) from the LINK variable of the coreutils Makefile. Afterwards, I run the following command:
make CC=/home/user/llvm-3.4.2/build/Release+Asserts/bin/clang
CCLD=/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-link
IGNORE_UNUSED_LIBRARIES_CFLAGS= CFLAGS="-emit-llvm -S"
VERBOSE=1 AM_CFLAGS= AM_LDFLAGS=
AR=/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-ar
RANLIB=/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-ranlib
and I get the following error:
/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-link: src/libver.a:1:2: error: expected integer
!<arch>
^
/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-link: error loading file 'src/libver.a'
Any ideas of how to get this to work?
Try this.
export CC="/home/user/llvm-3.4.2/build/Release+Asserts/bin/clang"
export CXX="/home/user/llvm-3.4.2/build/Release+Asserts/bin/clang++"
Make sure this is where your compiler toolchain is present.
Then in the the coreutils directory, run ./configure (before this run ./bootstrap if you havent already run it). Running ./configure checks if your clang can compile properly and creates a Makefile with the correct configuration.
Then do a make and make install as instructed.
Lib file '.a' here is not readable by llvm-link.
A possible informal hack to this probably is to find out the Makefile generating this lib, and let
AR = llvm-link, ar option = -o(i.e. change ar rv to llvm-link -o),
and disable ranlib command while compling(you don't need ranlib if using llvm-link).
Then the '.a' file generated is a stitched bc file, and this '.a' file should be accpetable by llvm-link command you are calling

using clang to generate call graph for a project

I have c and c++ project, and i would like to check for dead function (function that could not be called), for that i want to build a call graph and see which could not be accessed from the written code.
for that i want to use clang with the flag "-S -emit-llvm" so i could creat a dot file.
im using autoconf to compile the project and the autoconfig dont recognize the file that has been compiled as an executable.
tried using this line :
./configure --enable-debug --prefix=/opt/ibutils CC=clang CXX=clang++ CXXFLAGS="-S -emit-llvm"
and this
./configure --enable-debug --prefix=/opt/ibutils CC=clang CXX=clang++ CXXFLAGS="-S -emit-llvm"
LD="llvm-link"
does anyone know the reason? have a suggestions what could i do?
thanks

CMake preload script for cache

I fire cmake with the -C mysettings.cmake.
The content of myfile.cmake is
set(CMAKE_INSTALL_PREFIX "C:/install/mylib" STRING)
Everything is generated but it seems the -C mysettings.cmake variable is not set. It is still installed in the default directory.
Cmake prints the message "loading initial cache file ../../script/cmake/mysettings.cmake"
without any error.
The full call:
cmake -C ../../script/cmake/mysettings.cmake -G "Visual Studio 9 2008" ../../source/mylib
Is there something wrong with my syntax?
From the CMake manual:
The given file should be a CMake script containing SET commands that
use the CACHE option, not a cache-format file.
So your myfile.cmake needs to look something like this:
set(CMAKE_INSTALL_PREFIX "C:/install/mylib" CACHE PATH "")