Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
compile each object file only once...
cmake_minimum_required(VERSION 3.5)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()
set(program_name an_experiment)
project(${program_name} CXX)
set(VERSION "0.0.1")
function(set_common_properties_on_targets)
foreach(target_name ${ARGN})
message(STATUS "setting properties on: " ${target_name})
set_target_properties(${target_name} PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
target_compile_options(${target_name}
PUBLIC "-O2"
PUBLIC "-ggdb"
PUBLIC "-grecord-gcc-switches"
PUBLIC "-Wall"
PUBLIC "-Wextra"
PUBLIC "-fdiagnostics-color=auto")
target_include_directories(${target_name} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(${target_name} PUBLIC ${CMAKE_BINARY_DIR})
endforeach()
endfunction()
function(create_object_files)
foreach(src_files ${ARGN})
message(STATUS "adding an object file: " ${src_files})
string(REGEX REPLACE "\.cc$" "_o" name ${src_files})
string(REGEX REPLACE "\/" "_" name ${name})
add_library(${name} OBJECT ${src_files})
set_common_properties_on_targets(${name})
endforeach()
endfunction()
function(create_static_library)
list(GET ARGN 0 name)
list(REMOVE_AT ARGN 0)
set(add_library_arguments "")
foreach(src_file ${ARGN})
string(REGEX REPLACE "\.cc$" "_o" object_file_name ${src_file})
string(REGEX REPLACE "\/" "_" object_file_name ${object_file_name})
list(APPEND add_library_arguments "$<TARGET_OBJECTS:${object_file_name}>")
endforeach()
add_library(${name} STATIC ${add_library_arguments})
set_common_properties_on_targets(${name})
endfunction()
function(create_executable)
list(GET ARGN 0 name)
list(REMOVE_AT ARGN 0)
set(add_executable_arguments "")
foreach(src_file ${ARGN})
string(REGEX REPLACE "\.cc$" "_o" object_file_name ${src_file})
string(REGEX REPLACE "\/" "_" object_file_name ${object_file_name})
list(APPEND add_executable_arguments "$<TARGET_OBJECTS:${object_file_name}>")
endforeach()
add_executable(${name} ${add_executable_arguments})
set_common_properties_on_targets(${name})
endfunction()
create_object_files(
src/foo.cc
src/bar.cc
src/baz.cc
src/main.cc)
create_static_library(foobarbaz
src/foo.cc
src/bar.cc
src/baz.cc)
create_static_library(foobar
src/foo.cc
src/bar.cc)
create_static_library(foobaz
src/foo.cc
src/baz.cc)
create_static_library(barbaz
src/bar.cc
src/baz.cc)
create_static_library(baz
src/baz.cc)
create_static_library(bar
src/bar.cc)
create_static_library(foo
src/foo.cc)
create_executable(main
src/main.cc)
target_link_libraries(main
barbaz
foo)
target_include_directories(main PUBLIC src)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.