Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: compile a program twice in a row

To be able to do many automatic optimizations, I want to be able to compile my program using the flag -fprofile-generate first, then run it to generate the profile and then re-compile the program with -fprofile-use instead.

This means I want to compile my program twice in a row, with two different CMAKE_CXX_FLAGS each time.

How can I do that using CMake?

like image 381
CpCd0y Avatar asked Jul 23 '17 16:07

CpCd0y


1 Answers

You can build something and then run it and then build something else after execution by making use of customer targets and the "add_dependencies" command. For your gcov case you might 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)

Output showing build -> run -> build

Build Output

like image 132
partyd Avatar answered Nov 08 '22 07:11

partyd