CMake: compile program twice in a row
In order to be able to do many automatic optimizations, I want to first compile my program using the flag -fprofile-generate
and then run it to create the profile, then compile the program again with -fprofile-use
.
This means that I want to compile my program twice in a row with two different ones CMAKE_CXX_FLAGS
each time.
How can I do this using CMake?
+3
source to share
1 answer
You can build something and then run, and then compile something else after execution using the client targets and the "add_dependencies" command. For your gcov case, you can do something like:
profile.cxx
#include <iostream>
int main(void) {
std::cout << "Hello from Generating Profile run" << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(profileExample C CXX)
# compile initial program
add_executable(profileGenerate profile.cxx)
set_target_properties(profileGenerate PROPERTIES COMPILE_FLAGS "-fprofile-
generate")
target_link_libraries(profileGenerate gcov)
add_executable(profileUse profile.cxx)
set_target_properties(profileUse PROPERTIES COMPILE_FLAGS "-fprofile-use")
target_link_libraries(profileUse gcov)
# custom target to run program
add_custom_target(profileGenerate_run
COMMAND profileGenerate
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "run Profile Generate"
SOURCES profile.cxx
)
#create depency for profileUse on profileGenerate_run
add_dependencies(profileUse profileGenerate_run)
Result showing build -> run -> build
+1
source to share