Skip to content

Commit

Permalink
(Build) First pass over CMake (#151)
Browse files Browse the repository at this point in the history
* (Build) First pass over CMake

1. Requirement for CMake moves to version 3.10 (from 3.5)
1. Adds a `release` target that builds and installs all libs and programs
1. Factors CMake code into a `cmake` top-level folder 
1. Adds packaging for `.deb` and `.tgz`
1. Creates `IonC::` scope for CMake targets
1. Expands source and header files, avoiding CMake GLOB which can cause issues with caching 
1. Sets correct properties for libs and uses proper install destinations
  • Loading branch information
therapon authored Dec 3, 2019
1 parent 717fc05 commit 3131831
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 38 deletions.
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(IonC
VERSION 1.0.3
Expand All @@ -17,15 +17,29 @@ else()
add_compile_options("$<$<CONFIG:DEBUG>:-O0;-g3;-ggdb3;-Wall>")
endif()

include(GNUInstallDirs)

# NOTE: DECNUMDIGITS must be set across all compilation units to at least DECQUAD_Pmax (34), so that the value is
# guaranteed to be consistent between ionc and decNumber. This is required for conversions between decQuad and
# decNumber. This is NOT the limit on decimal precision; ION_DECIMAL can handle arbitrarily large precision.
add_definitions(-DDECNUMDIGITS=34)
message(STATUS "Setting DECNUMBER max digits to 34")

set(CMAKE_INSTALL_RPATH "$ORIGIN")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

add_subdirectory(decNumber)
add_subdirectory(ionc)
add_subdirectory(test)
add_subdirectory(tools)

export(PACKAGE IonC)

include(cmake/CMakeCPack.cmake)

## Convenience target to build and install
add_custom_target(release
COMMENT "Build and install the library"
COMMENT "Binary Dir : ${CMAKE_BINARY_DIR}"
COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target install
USES_TERMINAL)
34 changes: 34 additions & 0 deletions cmake/CMakeCPack.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VENDOR "The Ion Team")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/README.md")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "message: Ion library and tools")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")

set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/${PROJECT_NAME}")

set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")

set(CPACK_SOURCE_IGNORE_FILES
"${PROJECT_BINARY_DIR};/.idea;./.github;/.git/;.gitignore;.travis.yml;.appveyor.yml")
set(CPACK_SOURCE_GENERATOR "ZIP;TGZ")
set(CPACK_GENERATOR "ZIP;TGZ")

if(UNIX)
if(CMAKE_SYSTEM_NAME MATCHES Linux)
list(APPEND CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "ion-team")
set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
# set(CPACK_DEBIAN_PACKAGE_DEPENDS "uuid-dev")

list(APPEND CPACK_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_RELEASE "1")
set(CPACK_RPM_PACKAGE_LICENSE "Apache")
# set(CPACK_RPM_PACKAGE_REQUIRES "uuid-devel")
endif()
endif()

message(STATUS "CPack generators: ${CPACK_GENERATOR}")

include(CPack)
5 changes: 5 additions & 0 deletions cmake/IonCConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

if(NOT TARGET IonC::ionc)
include(${CMAKE_CURRENT_LIST_DIR}/IonCTargets.cmake)
endif()
49 changes: 33 additions & 16 deletions decNumber/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

add_library(decNumber
set(declibsrc
decContext.c
decimal128.c
decimal64.c
decimal32.c
decNumber.c
decPacked.c
decQuad.c
)
decQuad.c)

set(DEC_PUB_HEADERS
include/decNumber/decContext.h
include/decNumber/decDouble.h
include/decNumber/decDPD.h
include/decNumber/decimal128.h
include/decNumber/decimal32.h
include/decNumber/decimal64.h
include/decNumber/decNumber.h
include/decNumber/decNumberLocal.h
include/decNumber/decPacked.h
include/decNumber/decQuad.h
include/decNumber/decSingle.h)

add_library(decNumber ${declibsrc})
set_property(TARGET decNumber PROPERTY POSITION_INDEPENDENT_CODE 1)

target_include_directories(decNumber
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

set_target_properties(decNumber
PROPERTIES
PUBLIC_HEADER "${DEC_PUB_HEADERS}")


install(TARGETS decNumber
DESTINATION lib)

install(FILES include/decNumber/decContext.h
include/decNumber/decDouble.h
include/decNumber/decimal32.h
include/decNumber/decimal64.h
include/decNumber/decimal128.h
include/decNumber/decNumber.h
include/decNumber/decPacked.h
include/decNumber/decQuad.h
include/decNumber/decSingle.h
DESTINATION include/decNumber)
EXPORT IonCTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/decNumber)


install(DIRECTORY include/decNumber
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.h")
99 changes: 84 additions & 15 deletions ionc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

set(libsrc
decQuadHelpers.c
Expand Down Expand Up @@ -27,46 +26,116 @@ set(libsrc
ion_float.c
ion_extractor.c)

set(LIB_PUB_HEADERS
include/ionc/ion_catalog.h
include/ionc/ion_collection.h
include/ionc/ion_debug.h
include/ionc/ion_decimal.h
include/ionc/ion_error_codes.h
include/ionc/ion_errors.h
include/ionc/ion_extractor.h
include/ionc/ion_float.h
include/ionc/ion.h
include/ionc/ion_int.h
include/ionc/ion_platform_config.h
include/ionc/ion_reader.h
include/ionc/ion_stream.h
include/ionc/ion_string.h
include/ionc/ion_symbol_table.h
include/ionc/ion_timestamp.h
include/ionc/ion_types.h
include/ionc/ion_writer.h)



# this is the "object library" target: compiles the sources only once
add_library(objlib OBJECT ${libsrc})

# shared libraries need PIC
set_property(TARGET objlib PROPERTY POSITION_INDEPENDENT_CODE 1)
set_property(TARGET objlib
PROPERTY C_STANDARD 99)
set_target_properties(objlib
PROPERTIES
POSITION_INDEPENDENT_CODE 1
C_STANDARD 99)

target_include_directories(objlib
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../decNumber/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}

)


if (MSVC)
add_library(ionc $<TARGET_OBJECTS:objlib>)
else()
add_library(ionc SHARED $<TARGET_OBJECTS:objlib>)
add_library(ionc_static STATIC $<TARGET_OBJECTS:objlib>)
endif()


target_include_directories(objlib
PUBLIC
target_include_directories(ionc
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../decNumber/include>
PRIVATE
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}

)
add_library(ionc_static STATIC $<TARGET_OBJECTS:objlib>)
set_target_properties(ionc
PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION}
PUBLIC_HEADER "${LIB_PUB_HEADERS}")
endif()


if (MSVC)
target_link_libraries(ionc decNumber)
else()
# Unix requires linking against lib m explicitly.
target_link_libraries(ionc decNumber m)
target_link_libraries(ionc PUBLIC decNumber m)
endif()


set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/IonC)

install(TARGETS ionc
DESTINATION lib)
EXPORT IonCTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ionc)

install(EXPORT IonCTargets
FILE IonCTargets.cmake
NAMESPACE IonC::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/IonC)

install(DIRECTORY include/ionc
DESTINATION include/ionc
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.h")

####
## CMake packaging. Allows other CMake projects to detect and use IonC.
####
include(CMakePackageConfigHelpers)

message("CURRENT_SOURCE_DIR : ${CMAKE_CURRENT_SOURCE_DIR}")
message("SOURCE_DIR : ${CMAKE_SOURCE_DIR}")
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/../cmake/IonCConfig.cmake.in
${CMAKE_BINARY_DIR}/cmake/IonCConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/IonC)


write_basic_package_version_file(
${CMAKE_BINARY_DIR}/cmake/IonCConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)

install(
FILES
${CMAKE_BINARY_DIR}/cmake/IonCConfig.cmake
${CMAKE_BINARY_DIR}/cmake/IonCConfigVersion.cmake
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/IonC)
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# Avoid macro definition collisions between ionc and googletest.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_DONT_DEFINE_SUCCEED=1 -DGTEST_DONT_DEFINE_FAIL=1")
Expand Down
1 change: 0 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

add_subdirectory(ionizer)
add_subdirectory(ionsymbols)
Expand Down
1 change: 0 additions & 1 deletion tools/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

add_subdirectory(docopt)

Expand Down
1 change: 0 additions & 1 deletion tools/events/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
add_library(ion_events
ion_event_util.cpp
ion_event_stream.cpp
Expand Down
1 change: 0 additions & 1 deletion tools/ionizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

add_executable(ionizer
ionizer_args.c
Expand Down
1 change: 0 additions & 1 deletion tools/ionsymbols/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

add_executable(ionsymbols
ionsymbols_args.c
Expand Down

0 comments on commit 3131831

Please sign in to comment.