Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced CMake install features #112

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.cmake.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1 cmake options
-DBUILD_SHARED_LIB=(OFF|ON) default: OFF
-DCMAKE_BUILD_TYPE=(release|debug) default:release

2 build options
make install or Visual Studio target INSTALL will install regxmllib

3 using regxmllib in other applications
add
find_package(regxmllibc)
target_link_libraries(${EXECUTABLE} general regxmllibc)
to application's CMakeLists.txt


79 changes: 70 additions & 9 deletions regxmllib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,92 @@
cmake_minimum_required (VERSION 3.5)
cmake_minimum_required (VERSION 3.6)
project (regxmllibc)
option(BUILD_SHARED_LIB Bool OFF)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake already defines BUILD_SHARED_LIBS

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced for pure convenience when using a CMake GUI, from [1]:
This variable is often added to projects as an option() so that each user of a project can decide if they want to build the project using shared or static libraries

[1] https://cmake.org/cmake/help/v3.5/variable/BUILD_SHARED_LIBS.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok... but it says BUILD_SHARED_LIB and not BUILD_SHARED_LIBS.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I forgot. It's a locally defined name.

# Set build type
if(BUILD_SHARED_LIB)
set(LIB_TYPE SHARED)
if(WIN32 AND NOT CYGWIN)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be set for all platforms... or does it cause problems in cygwin?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's being set specifically for WIN32 DLLs. I did not test with cygwin and have it therefore explicitly excluded.

endif(WIN32 AND NOT CYGWIN)
else(BUILD_SHARED_LIB)
set(LIB_TYPE STATIC)
add_definitions( -DUTILS_LIB_STATIC )
endif(BUILD_SHARED_LIB)
message(STATUS "*** LIB_TYPE is ${LIB_TYPE}")

include(FindXercesC)
find_package(XercesC REQUIRED)
include_directories( ${XercesC_INCLUDE_DIR}
src/main/cpp
)

# Append "_d" if debug lib.
set(CMAKE_DEBUG_POSTFIX _d)

file(GLOB_RECURSE SRC_FILES src/main/cpp/*.cpp src/main/cpp/*.h )
add_library(${PROJECT_NAME} ${SRC_FILES})
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 98)

file(GLOB_RECURSE SRC_FILES src/main/cpp/*.cpp )
file(GLOB_RECURSE INC_FILES src/main/cpp/*.h )
add_library(${PROJECT_NAME} ${LIB_TYPE} ${SRC_FILES} ${INC_FILES})

set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 98)

target_link_libraries ( ${PROJECT_NAME} ${XercesC_LIBRARY} )

foreach(source IN LISTS SRC_FILES)
# Set Visual Studio filters for nested sourcce paths
foreach(source IN LISTS SRC_FILES INC_FILES)
file(RELATIVE_PATH rel_source ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/com/ ${source})
get_filename_component(source_path ${rel_source} DIRECTORY)
string(REPLACE "\\" "/" source_path_msvc "${source_path}")
source_group("${source_path_msvc}" FILES "${source}")
endforeach()

if(WIN32 AND NOT CYGWIN)
set(DEF_INSTALL_CMAKE_DIR CMake)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be set by the user?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulting INSTALL_CMAKE_DIR can be edited by the user after configuring the build and before generating the build files. DEF_INSTALL_CMAKE_DIR holds useful default locations.

else()
set(DEF_INSTALL_CMAKE_DIR lib/CMake/${PROJECT_NAME})
endif()
set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
"Installation directory for CMake files")
set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
set(INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")

# Make relative paths absolute (needed later on)
foreach(p LIB BIN INCLUDE CMAKE)
set(var INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif()
endforeach()

# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry)
export(PACKAGE ${PROJECT_NAME})

# Create the ${PROJECT_NAME}Config.cmake file in the install tree
file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
set(CONF_INCLUDE_DIRS "\${REGXMLLIBC_CMAKE_DIR}/${REL_INCLUDE_DIR}")
configure_file(${PROJECT_NAME}Config.cmake.in
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake" @ONLY)


install(EXPORT ${PROJECT_NAME}Targets DESTINATION
${INSTALL_CMAKE_DIR} COMPONENT dev)

install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Targets DESTINATION ${INSTALL_LIB_DIR}
)

foreach ( file ${INC_FILES} )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this portion of the cmake file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looping through all include files and installing them with their respective full relative path. lines 81 and 82 are debug output and should be removed, though.

get_filename_component( dir ${file} DIRECTORY )
file(RELATIVE_PATH dir2 "${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp" ${dir})
message(STATUS ${dir})
message(STATUS ${dir2})
install( FILES ${file} DESTINATION "${INSTALL_INCLUDE_DIR}/${PROJECT_NAME}/${dir2}" )
endforeach()


install(FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake"
DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)


#unit tests
Expand All @@ -45,5 +108,3 @@ endforeach( UNIT_TEST_PATH ${UNIT_TESTS} )
#add_executable(AUIDTest src/test/cpp/com/sandflow/smpte/util/AUIDTest.cpp)
#target_link_libraries(AUIDTest regxmllibc)
#add_test(AUIDTest AUIDTest)

install(DIRECTORY src/main/cpp/com/sandflow DESTINATION include FILES_MATCHING PATTERN "*.h")
18 changes: 18 additions & 0 deletions regxmllib/regxmllibcConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# REGXMLLIBC_INCLUDE_DIRS - include directories for regxmllibc
# REGXMLLIBC_LIBRARIES - libraries to link against

# Compute paths
get_filename_component(REGXMLLIBC_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
# set(regxmllibc_INCLUDE_DIRS "@CONF_INCLUDE_DIRS@/regxmllibc")
set(REGXMLLIBC_INCLUDE_DIRS "@CONF_INCLUDE_DIRS@/regxmllibc")

# Our library dependencies (contains definitions for IMPORTED targets)
if(NOT TARGET regxmllibc AND NOT regxmllibc_BINARY_DIR)
include("${REGXMLLIBC_CMAKE_DIR}/regxmllibcTargets.cmake")
endif()

# These are IMPORTED targets created by regxmllibcTargets.cmake
set(REGXMLLIBC_LIBRARIES
"$<$<NOT:$<CONFIG:Debug>>:${PROJECT_NAME}>"
"$<$<CONFIG:Debug>:${PROJECT_NAME}${CMAKE_DEBUG_POSTFIX}>"
)