Skip to content

Commit

Permalink
GHI #11 Setup CMake (#12)
Browse files Browse the repository at this point in the history
## Scope

- create core source files (`includes/patomic/patomic.h` and
`src/patomic.c`)
- setup CMake to build and install library

---------

Signed-off-by: doodspav <[email protected]>
  • Loading branch information
doodspav authored Mar 12, 2023
1 parent 731469a commit e9d6ff4
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 0 deletions.
81 changes: 81 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
cmake_minimum_required(VERSION 3.14)

include(cmake/util/PreventInSourceBuilds.cmake)

project(
patomic
VERSION 0.5.1
DESCRIPTION "Portable C90 Atomics Library"
HOMEPAGE_URL "https://github.com/doodspav/patomic"
LANGUAGES C
)

# don't change include order, OptionVariables checks if project is top level
include(cmake/util/ProjectIsTopLevel.cmake)
include(cmake/util/OptionVariables.cmake)


# ---- Declare Library ----

add_library(
patomic_patomic
${patomic_BUILD_TYPE}
# include
include/patomic/patomic.h
# src
src/patomic.c
)

# alias to cause error at configuration time instead of link time if target is missing
add_library(patomic::patomic ALIAS patomic_patomic)


# ---- Generate Build Info Headers ----

# used in export header generated below
if(NOT patomic_BUILD_SHARED)
target_compile_definitions(patomic_patomic PUBLIC PATOMIC_STATIC_DEFINE)
endif()

include(GenerateExportHeader)
generate_export_header(
patomic_patomic
BASE_NAME patomic
EXPORT_FILE_NAME include/patomic/patomic_export.h
)


# ---- Library Properties ----

set_target_properties(
patomic_patomic PROPERTIES
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}"
EXPORT_NAME patomic
OUTPUT_NAME patomic
)

# header files generated by CMake
target_include_directories(
patomic_patomic SYSTEM
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"
)

# header files from /include
target_include_directories(
patomic_patomic ${patomic_WARNING_GUARD}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
)

target_compile_features(patomic_patomic PUBLIC c_std_90)


# ---- Install Rules ----

if(NOT CMAKE_SKIP_INSTALL_RULES)
include(cmake/util/InstallRules.cmake)
endif()
1 change: 1 addition & 0 deletions cmake/util/InstallConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/patomicTargets.cmake")
77 changes: 77 additions & 0 deletions cmake/util/InstallRules.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
if(PROJECT_IS_TOP_LEVEL)
set(
CMAKE_INSTALL_INCLUDEDIR "include/patomic-${PROJECT_VERSION}"
CACHE PATH ""
)
endif()

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

# find_package(<package>) call for consumers to find this project
set(package patomic)

# copy header files to CMAKE_INSTALL_INCLUDEDIR
install(
DIRECTORY
"${PROJECT_SOURCE_DIR}/include/"
"${PROJECT_BINARY_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
COMPONENT patomic_Development
)

# copy target build output artifacts to OS dependent locations
install(
TARGETS patomic_patomic
EXPORT patomicTargets
RUNTIME #
COMPONENT patomic_Runtime
LIBRARY #
COMPONENT patomic_Runtime
NAMELINK_COMPONENT patomic_Development
ARCHIVE #
COMPONENT patomic_Development
INCLUDES #
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

# allow package maintainers to freely override the path for the configs
set(
patomic_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${package}"
CACHE PATH "CMake package config location relative to the install prefix"
)
mark_as_advanced(patomic_INSTALL_CMAKE_DIR)

# copy config file for find_package to find
install(
FILES "${PROJECT_SOURCE_DIR}/cmake/util/InstallConfig.cmake"
DESTINATION "${patomic_INSTALL_CMAKEDIR}"
RENAME "${package}Config.cmake"
COMPONENT patomic_Development
)

# create version file for consumer to check version in CMake
write_basic_package_version_file(
"${package}ConfigVersion.cmake"
COMPATIBILITY SameMajorVersion
)

# copy version file for find_package to find
install(
FILES "${PROJECT_BINARY_DIR}/${package}ConfigVersion.cmake"
DESTINATION "${patomic_INSTALL_CMAKEDIR}"
COMPONENT patomic_Development
)

# create core configuration file detailing targets for consumer
install(
EXPORT patomicTargets
NAMESPACE patomic::
DESTINATION "${patomic_INSTALL_CMAKEDIR}"
COMPONENT patomic_Development
)

# support packaging library
if(PROJECT_IS_TOP_LEVEL)
include(CPack)
endif()
37 changes: 37 additions & 0 deletions cmake/util/OptionVariables.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ---- Build Shared ----

# Sometimes it's useful to be able to single out a dependency to be built as
# static or shared, even if obtained from source.
if(PROJECT_IS_TOP_LEVEL)
option(BUILD_SHARED_LIBS "Build shared libs" OFF)
endif()
option(
patomic_BUILD_SHARED
"Override BUILD_SHARED_LIBS for patomic library"
${BUILD_SHARED_LIBS}
)
mark_as_advanced(patomic_BUILD_SHARED)
set(patomic_BUILD_TYPE STATIC)
if(patomic_BUILD_SHARED)
set(patomic_BUILD_TYPE SHARED)
endif()


# ---- Warning Guard ----

# target_include_directories with SYSTEM modifier will request the compiler to
# omit warnings from the provided paths, if the compiler supports that.
# This is to provide a user experience similar to find_package when
# add_subdirectory or FetchContent is used to consume this project.
set(patomic_WARNING_GUARD "")
if(NOT PROJECT_IS_TOP_LEVEL)
option(
patomic_INCLUDES_WITH_SYSTEM
"Use SYSTEM modifier for patomic's includes, disabling warnings"
ON
)
mark_as_advanced(patomic_INCLUDES_WITH_SYSTEM)
if(patomic_INCLUDES_WITH_SYSTEM)
set(patomic_WARNING_GUARD SYSTEM)
endif()
endif()
9 changes: 9 additions & 0 deletions cmake/util/PreventInSourceBuilds.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# in-source build guard
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(
FATAL_ERROR
"In-source builds are not supported. "
"Please read the BUILDING document before trying to build this project. "
"You may need to delete 'CMakeCache.txt' and 'CMakeFiles/' first."
)
endif()
6 changes: 6 additions & 0 deletions cmake/util/ProjectIsTopLevel.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# this variable is set by project() in CMake 3.21+
string(
COMPARE EQUAL
"${CMAKE_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}"
PROJECT_IS_TOP_LEVEL
)
22 changes: 22 additions & 0 deletions include/patomic/patomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef PATOMIC_PATOMIC_H
#define PATOMIC_PATOMIC_H

#include <patomic/patomic_export.h>

#ifdef __cplusplus
extern "C" {
#endif


PATOMIC_EXPORT int
patomic_example_add(
int a,
int b
);


#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* PATOMIC_PATOMIC_H */
11 changes: 11 additions & 0 deletions src/patomic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <patomic/patomic.h>


PATOMIC_EXPORT int
patomic_example_add(
int a,
int b
)
{
return a + b;
}

0 comments on commit e9d6ff4

Please sign in to comment.