Skip to content

Commit

Permalink
CMake work
Browse files Browse the repository at this point in the history
  • Loading branch information
JJL772 committed Jan 29, 2025
1 parent 1262ba9 commit acd95d4
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 4 deletions.
59 changes: 57 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,46 @@ set(WITH_TIRPC OFF CACHE BOOL "")

include(FetchContent)
include(CheckIncludeFile)
include(GNUInstallDirs)

# Always build with -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(PkgConfig REQUIRED)

####################################################
# Boost, Python and Cython
####################################################

set(NO_PYTHON 0)
if ("${WITH_PYCPSW}" STREQUAL "BOOST")
find_package(
boost COMPONENTS Python
Boost REQUIRED COMPONENTS Python
)
message(STATUS "Using BOOST for Python bindings")
elseif ("${WITH_PYCPSW}" STREQUAL "CYTHON")
include(cmake/FindCython.cmake)
find_package(
cython
cython REQUIRED
)
message(STATUS "Using Cython for Python bindings")
elseif ("${WITH_PYCPSW}" STREQUAL "NO")
set(NO_PYTHON 1)
message(STATUS "CPSW Python bindings are disabled")
else()
message(FATAL_ERROR "Invalid choice, WITH_PYCPSW must be BOOST, CYTHON or NO")
endif()

if (NOT NO_PYTHON)
find_package(
Python REQUIRED COMPONENTS Development
)
endif()

####################################################
# tirpc
####################################################

# Check for RPC library
check_include_file(rpc/rpc.h HAVE_RPC)

Expand All @@ -42,6 +68,9 @@ if (WITH_CXX11)
set(CMAKE_CXX_STANDARD 11)
endif()

####################################################
# yaml-cpp
####################################################

FetchContent_Declare(
yaml-cpp
Expand All @@ -50,5 +79,31 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(yaml-cpp)

####################################################
# Misc
####################################################

# Generate a version string
execute_process(
COMMAND bash -c "echo -n \\\"`git describe --always --dirty`\\\""
OUTPUT_VARIABLE CPSW_GIT_VERSION_STRING
)

message(STATUS "CPSW_GIT_VERSION_STRING=${CPSW_GIT_VERSION_STRING}")

string(REGEX MATCH "\"?R([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)" CPSW_API_VERSION "${CPSW_GIT_VERSION_STRING}")

set(CPSW_API_VERSION "${CMAKE_MATCH_1}")
set(CPSW_MAJOR_VERSION "${CMAKE_MATCH_1}")
set(CPSW_MINOR_VERSION "${CMAKE_MATCH_2}")
set(CPSW_REVISION "${CMAKE_MATCH_3}")
set(CPSW_DELTA_COMMIT "${CMAKE_MATCH_4}")

message(STATUS "CPSW_API_VERSION=${CPSW_API_VERSION}")
message(STATUS "CPSW_MAJOR_VERSION=${CPSW_MAJOR_VERSION}")
message(STATUS "CPSW_MINOR_VERSION=${CPSW_MINOR_VERSION}")
message(STATUS "CPSW_REVISION=${CPSW_REVISION}")
message(STATUS "CPSW_DELTA_COMMIT=${CPSW_DELTA_COMMIT}")


add_subdirectory(src)
179 changes: 177 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,48 @@ set(CPSW_FRAMEWORK_SRCS
cpsw_stdio.h
)

# CPSW public API headers
set(CPSW_FRAMEWORK_HEADERS
cpsw_api_user.h
cpsw_api_builder.h
cpsw_api_timeout.h
cpsw_error.h
cpsw_stdio.h
${CMAKE_CURRENT_BINARY_DIR}/generated/cpsw_api_version.h
)

#############################################################
# CPSW
#############################################################

if (WITH_CXX11)
list(APPEND CPSW_FRAMEWORK_HEADERS std/cpsw_shared_ptr.h)
else()
list(APPEND CPSW_FRAMEWORK_HEADERS boost/cpsw_shared_ptr.h)
endif()

# Generate the API version header
configure_file(
cpsw_api_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/cpsw_api_version.h
)

# Generate GIT version header
configure_file(
cpsw_git_version_string.h.in ${CMAKE_CURRENT_BINARY_DIR}/generated/cpsw_git_version_string.h
)

add_library(
cpsw STATIC
${CPSW_FRAMEWORK_SRCS}
)


target_link_libraries(
cpsw PUBLIC

yaml-cpp::yaml-cpp
)

# Link against TIRPC if required
if (WITH_TIRPC)
target_link_libraries(
cpsw PUBLIC
Expand All @@ -141,7 +171,14 @@ if (WITH_TIRPC)
endif()

target_include_directories(
cpsw PRIVATE "${CMAKE_CURRENT_LIST_DIR}"
cpsw PUBLIC
"${CMAKE_CURRENT_LIST_DIR}"
"${CMAKE_CURRENT_BINARY_DIR}/generated"
)

target_compile_definitions(
cpsw PRIVATE
"-DGIT_RELEASE_TAG=${CPSW_GIT_VERSION_STRING}"
)

# C++11 specific includes for CPSW
Expand All @@ -157,3 +194,141 @@ else()
)
endif()

# Install CPSW headers
install(
FILES ${CPSW_FRAMEWORK_HEADERS}
TYPE INCLUDE
)

# Install CPSW library
install(
TARGETS cpsw
)

#############################################################
# pycpsw & pyyaml
#############################################################
if ("${WITH_PYCPSW}" STREQUAL "BOOST")
set(PYCPSW_SRCS
cpsw_boost_python.cc
cpsw_python.cc
)

set(PYYAML_SRCS
yaml_cpp_python.cc
)

set(
PYCPSW_LIBS
Boost::python
)
elseif ("${WITH_PYCPSW}" STREQUAL "CYTHON")
set(PYCPSW_SRCS
cpsw_python.cc
pycpsw.cc
cpsw_cython.cc
)

set(PYYAML_SRCS
pyyaml_cpp.cc
)

set(
PYCPSW_LIBS
)
endif()

if (NOT "${WITH_PYCPSW}" STREQUAL "NO")
add_library(
pycpsw SHARED ${PYCPSW_SRCS}
)

add_library(
yaml_cpp SHARED ${PYYAML_SRCS}
)

target_link_libraries(
pycpsw PUBLIC cpsw Python::Python ${PYCPSW_LIBS}
)

target_link_libraries(
yaml_cpp PUBLIC cpsw Python::Python ${PYCPSW_LIBS}
)

target_compile_options(
pycpsw PRIVATE -fno-strict-aliasing
)

target_compile_options(
yaml_cpp PRIVATE -fno-strict-aliasing
)

target_compile_definitions(
pycpsw PRIVATE WITH_PYCPSW=PYCPSW_${WITH_PYCPSW}
)

target_compile_definitions(
yaml_cpp PRIVATE WITH_PYCPSW=PYCPSW_${WITH_PYCPSW}
)

# A bit unconventional; needs to go in the bin directory for runtime use
install(
TARGETS pycpsw yaml_cpp
DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Disable lib prefix for these binaries
set_target_properties(
pycpsw yaml_cpp PROPERTIES PREFIX ""
)
endif()

#############################################################
# cpsw_yaml_xpand
#############################################################

add_executable(
cpsw_yaml_xpand

cpsw_yaml_xpand.cc
)

target_link_libraries(
cpsw_yaml_xpand PRIVATE cpsw
)

install(TARGETS cpsw_yaml_xpand)

#############################################################
# cpsw_yaml_merge
#############################################################

add_executable(
cpsw_yaml_merge

cpsw_yaml_merge.cc
cpsw_yaml_merge_main.cc
cpsw_stdio.cc
)

target_link_libraries(
cpsw_yaml_merge PRIVATE cpsw
)

install(TARGETS cpsw_yaml_merge)

#############################################################
# cpsw_ypp
#############################################################

add_executable(
cpsw_ypp

cpsw_ypp.cc
)

target_link_libraries(
cpsw_ypp PRIVATE cpsw
)

install(TARGETS cpsw_ypp)
15 changes: 15 additions & 0 deletions src/cpsw_api_version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@C Copyright Notice
//@C ================
//@C This file is part of CPSW. It is subject to the license terms in the LICENSE.txt
//@C file found in the top-level directory of this distribution and at
//@C https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
//@C
//@C No part of CPSW, including this file, may be copied, modified, propagated, or
//@C distributed except according to the terms contained in the LICENSE.txt file.

#ifndef _CPSW_API_VERSION_H_
#define _CPSW_API_VERSION_H_

#define CPSW_API_VERSION ${CPSW_API_VERSION}

#endif // _CPSW_API_VERSION_H_
19 changes: 19 additions & 0 deletions src/cpsw_git_version_string.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@C Copyright Notice
//@C ================
//@C This file is part of CPSW. It is subject to the license terms in the LICENSE.txt
//@C file found in the top-level directory of this distribution and at
//@C https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
//@C
//@C No part of CPSW, including this file, may be copied, modified, propagated, or
//@C distributed except according to the terms contained in the LICENSE.txt file.

#ifndef CPSW_GIT_VERSION_STRING_H
#define CPSW_GIT_VERSION_STRING_H

#define CPSW_GIT_VERSION_STRING ${CPSW_GIT_VERSION_STRING}
#define CPSW_MAJOR_VERSION ${CPSW_MAJOR_VERSION}
#define CPSW_MINOR_VERSION ${CPSW_MINOR_VERSION}
#define CPSW_REVISION ${CPSW_REVISION}
#define CPSW_DELTA_COMMIT ${CPSW_DELTA_COMMIT}

#endif

0 comments on commit acd95d4

Please sign in to comment.