cmake: support BUILD_SHARED_LIBS built-in option

Support building internal libraries as shared. This reduces
development time by eliminating the need to re-link all
binaries every time non-interface code in the library changes.
Instead, can hack on libxyz, then `make libxyz`, and re-run
monerod.

By default BUILD_SHARED_LIBS is OFF in release build type,
and ON in debug build type, but can be overriden with -D.
This commit is contained in:
redfish 2016-09-17 14:03:51 -04:00
parent e1c7af35d4
commit 06bb6923c3
3 changed files with 16 additions and 4 deletions

View file

@ -49,6 +49,7 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
message(STATUS "Setting default build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Setting default build type: ${CMAKE_BUILD_TYPE}")
endif() endif()
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
# ARCH defines the target architecture, either by an explicit identifier or # ARCH defines the target architecture, either by an explicit identifier or
# one of the following two keywords. By default, ARCH a value of 'native': # one of the following two keywords. By default, ARCH a value of 'native':
@ -179,6 +180,17 @@ else()
endif() endif()
option(STATIC "Link libraries statically" ${DEFAULT_STATIC}) option(STATIC "Link libraries statically" ${DEFAULT_STATIC})
# This is a CMake built-in switch that concerns internal libraries
if (NOT DEFINED BUILD_SHARED_LIBS AND NOT STATIC AND CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
set(BUILD_SHARED_LIBS ON CACHE STRING "Build internal libs as shared")
endif()
if (BUILD_SHARED_LIBS)
message(STATUS "Building internal libraries as shared")
set(PIC_FLAG "-fPIC")
else()
message(STATUS "Building internal libraries as static")
endif()
if(MINGW) if(MINGW)
string(REGEX MATCH "^[^/]:/[^/]*" msys2_install_path "${CMAKE_C_COMPILER}") string(REGEX MATCH "^[^/]:/[^/]*" msys2_install_path "${CMAKE_C_COMPILER}")
message(STATUS "MSYS location: ${msys2_install_path}") message(STATUS "MSYS location: ${msys2_install_path}")
@ -365,8 +377,8 @@ else()
set(COVERAGE_FLAGS "-fprofile-arcs -ftest-coverage --coverage") set(COVERAGE_FLAGS "-fprofile-arcs -ftest-coverage --coverage")
endif() endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG} ${COVERAGE_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG} ${COVERAGE_FLAGS} ${PIC_FLAG}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG} ${COVERAGE_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG} ${COVERAGE_FLAGS} ${PIC_FLAG}")
# With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that # With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that
# is fixed in the code (Issue #847), force compiler to be conservative. # is fixed in the code (Issue #847), force compiler to be conservative.

View file

@ -9,6 +9,6 @@ file(GLOB otshell_utils_sources # All files in directory:
"*.cpp" "*.cpp"
) )
add_library (otshell_utils STATIC ${otshell_utils_sources}) add_library (otshell_utils ${otshell_utils_sources})
set_target_properties (otshell_utils PROPERTIES OUTPUT_NAME "otshell_utils") set_target_properties (otshell_utils PROPERTIES OUTPUT_NAME "otshell_utils")
#target_link_libraries (upnpc-static ${LDLIBS}) # to add used libs #target_link_libraries (upnpc-static ${LDLIBS}) # to add used libs

View file

@ -88,7 +88,7 @@ function (bitmonero_add_library name)
# libwallet, which combines multiple components. # libwallet, which combines multiple components.
set(objlib obj_${name}) set(objlib obj_${name})
add_library(${objlib} OBJECT ${ARGN}) add_library(${objlib} OBJECT ${ARGN})
add_library("${name}" STATIC $<TARGET_OBJECTS:${objlib}>) add_library("${name}" $<TARGET_OBJECTS:${objlib}>)
set_property(TARGET "${name}" set_property(TARGET "${name}"
PROPERTY PROPERTY
FOLDER "libs") FOLDER "libs")