简体   繁体   中英

CMake, UnitBuild specific compile options

I have a project structured in folders:

ProjectRoot/
ProjectRoot/Folder1
ProjectRoot/Folder2

Currenty my Cmake file located in ProjectRoot looks like that

#I'm not proficient with Cmake, so I force recent version to prevent me debuggin
#problems for users that use old Cmake versions.

cmake_minimum_required( VERSION 2.8)
project( Project)

add_definitions( -DPROJECT_BUILD_DLL)


if(MINGW)
    add_compile_options( -Os -Wall -Wextra)
endif()

add_subdirectory( ProjectRoot/Folder1)
add_subdirectory( ProjectRoot/Folder2)


add_library( libProject SHARED $<TARGET_OBJECTS:ProjectRootObj>
                               $<TARGET_OBJECTS:ProjectRootFolder1Obj>)

And for each subfolder I have a file like that:

cmake_minimum_required( VERSION 2.8)
project( ProjectRoot_Folder1)

# find source files
file(GLOB sourceFiles
"*.cpp"
)

# Exclude them from build
set_source_files_properties(${sourceFiles} PROPERTIES HEADER_FILE_ONLY true)

# Create single source file
set(unit_build_file ${CMAKE_CURRENT_BINARY_DIR}/all.cpp)
file( WRITE ${unit_build_file} "// autogenerated by CMake\n")

foreach(source_file ${sourceFiles} )
    file( APPEND ${unit_build_file} "#include \"${source_file}\"\n")
endforeach(source_file)



# Add compiler and unit-build specific settings
if(MINGW)
  add_compile_options( -Wzero-as-null-pointer-constant  
                       -Wold-style-cast
                   )
endif()

add_library( ProjectRootFolder1Obj OBJECT all.cpp )

The build is successfull. However I have a nasty issue, compiler options setted in subfolders are applied "project-wide" (options from every CMakeLists.txt file are "added" to other files as well!)

The source code in Folder2 is an autogenerated OpenGL source file(GLLoadGen) and so I want it to compile without the compile options (wich are set in another folder from another CMakeLists.txt file):

-Wzero-as-null-pointer-constant  
-Wold-style-cast

Because it is generating hundreds warnings. regardless of wich order I add subfolders, seems that "compiler options" are applied project wide, that means that if I compile

Folder 1 with

-O1

Folder 2 with

-O2

by looking at generated Makefiles, I see that both "-O1" and "-O2" are applied as command line options for both folders! while instead I want to use a different compile option for each folder because each folder is a different compile unit that need different warnings and optimizazione levels.

This seems a Cmake issue to me because I followed their tutorial about OBJECT targets wich specifically states "using different compiler options for each object". So what am I missing?

Credits:

  • I always used unity builds for my projects, I'm using now Cmake to automate "all.cpp" generation (I did that with bash script before) using the tutorial at this page: enter link description here

After investigatin I finally found the solution. The problem is not the relative order of sub_folders, but the order of "add_compile_options". To achieve different compile options for each subdirectory I have to do the following:

#options seen by subfolders
if(MINGW)
    add_compile_options( -Os -Wall -Wextra) 
endif()

add_subdirectory( ProjectRoot/Folder1)
add_subdirectory( ProjectRoot/Folder2)

#options seen only by current target (if any file compiled here)
if(MINGW)
    add_compile_options( -Wzero-as-null-pointer-constant  
                         -Wold-style-cast)
endif()

The problem was that some folders were added by CMakeLists.txt wich had "add_compile_options" in the wrong place.

Basically my solution/advice on that: avoid recursively add CMakeLists.txt Use a root "CMakeLists.txt" and include all subfolders from that. That keep projects much simpler (you have one place to edit when adding new folders instead of finding N files to be edited to include their "children")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM