Skip to content

Commit

Permalink
Add custom CMake flags to external projects
Browse files Browse the repository at this point in the history
  • Loading branch information
fdevinc committed Jan 29, 2024
0 parents commit cd0040a
Show file tree
Hide file tree
Showing 51 changed files with 9,542 additions and 0 deletions.
87 changes: 87 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 100
CommentPragmas: '(.+\n*.+)*'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 2
- Regex: '^<.*'
Priority: 1
- Regex: '.*'
Priority: 3
IndentCaseLabels: true
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
SortIncludes: true
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never
...
45 changes: 45 additions & 0 deletions .github/workflows/ungar-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
pull_request:

env:
BUILD_TYPE: Release

jobs:
ci:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
compiler: [ gcc-11, gcc-13 ]

runs-on: ${{ matrix.os }}
steps:
- name: Check out Ungar
uses: actions/checkout@v3

- name: Install compiler
id: install_compiler
uses: rlalik/setup-cpp-compiler@v1.2
with:
compiler: ${{ matrix.compiler }}

- name: Configure CMake
run: cmake -DUNGAR_BUILD_TESTS=ON
-DUNGAR_BUILD_EXAMPLES=ON
-DUNGAR_ENABLE_LOGGING=ON
-DUNGAR_ENABLE_AUTODIFF=ON
-DUNGAR_ENABLE_OPTIMIZATION=ON
-B ${{github.workspace}}/build
-DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
-DCMAKE_C_COMPILER=${{ steps.install_compiler.outputs.cc }}
-DCMAKE_CXX_COMPILER=${{ steps.install_compiler.outputs.cxx }}

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -C ${{env.BUILD_TYPE}}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/.cache/
174 changes: 174 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
cmake_minimum_required(VERSION 3.20)

project(Ungar VERSION 1.0.1)

add_library(ungar INTERFACE)
add_library(ungar::ungar ALIAS ungar)
target_compile_features(ungar INTERFACE cxx_std_20)

# ##############################################################################
# Set up CMake options.
# ##############################################################################
include(CMakeDependentOption)

option(
UNGAR_USE_SYSTEM_LIBRARIES
"Prevent automatic download of missing external dependencies. To selectively use system-wide packages, provide their installation prefixes as CMake variables of the form '<PackageName>_ROOT', where <PackageName> is the (case-preserved) name given to the find_package() call and _ROOT is literal."
OFF)
option(UNGAR_BUILD_TESTS "Add targets for unit testing." OFF)
option(UNGAR_BUILD_EXAMPLES "Add targets showing Ungar's features." OFF)
option(UNGAR_INSTALL "Install CMake targets during install step." OFF)
option(UNGAR_ENABLE_OPTIMIZATION
"Enable Ungar's nonlinear programming interface." OFF)
cmake_dependent_option(
UNGAR_ENABLE_LOGGING
"Enable Ungar's logging functionalities. Automatically enabled if any of the flags 'UNGAR_ENABLE_AUTODIFF', 'UNGAR_ENABLE_OPTIMIZATION', and 'UNGAR_BUILD_EXAMPLES' is set to 'ON'."
OFF
"NOT UNGAR_ENABLE_AUTODIFF;NOT UNGAR_ENABLE_OPTIMIZATION;NOT UNGAR_BUILD_EXAMPLES"
ON)
cmake_dependent_option(
UNGAR_ENABLE_AUTODIFF
"Enable Ungar's autodiff interface. Automatically enabled if 'UNGAR_ENABLE_OPTIMIZATION' is set to 'ON'."
OFF
"NOT UNGAR_ENABLE_OPTIMIZATION"
ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

message(STATUS "--------------------------------------------------")
message(STATUS "Configuring Ungar with the following options:")
message(STATUS " Enable logging : ${UNGAR_ENABLE_LOGGING}")
message(STATUS " Enable autodiff : ${UNGAR_ENABLE_AUTODIFF}")
message(STATUS " Enable optimization : ${UNGAR_ENABLE_OPTIMIZATION}")
message(STATUS " Build examples : ${UNGAR_BUILD_EXAMPLES}")
message(STATUS " Build tests : ${UNGAR_BUILD_TESTS}")
message(STATUS " Install : ${UNGAR_INSTALL}")
message(STATUS " Use system libraries : ${UNGAR_USE_SYSTEM_LIBRARIES}")

# Add external dependencies.
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
add_subdirectory(external)
message(STATUS "--------------------------------------------------")

# ##############################################################################
# Add compile options.
# ##############################################################################
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") # Clang.
target_compile_options(ungar INTERFACE "-pedantic" "$<$<CONFIG:RELEASE>:-O2>")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # GNU Compiler Collection (GCC).
target_compile_options(
ungar
INTERFACE "-pedantic"
"-ftemplate-backtrace-limit=1"
"-fconstexpr-depth=2147483647"
"-fconstexpr-loop-limit=2147483647"
"-fconstexpr-cache-depth=2147483647"
"-fconstexpr-ops-limit=2147483647"
"$<$<CONFIG:RELEASE>:-O3>")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") # Intel C++ Compiler.
message(WARNING "Intel C++ Compiler is not supported.")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # MSVC.
target_compile_options(ungar INTERFACE "/W4" "$<$<CONFIG:RELEASE>:/O2>")
else()
message(WARNING "${CMAKE_CXX_COMPILER_ID} is not supported.")
endif()

# ##############################################################################
# Set Ungar's include directories and link libraries.
# ##############################################################################
# Core modules.
list(APPEND UNGAR_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
list(APPEND UNGAR_LINK_LIBRARIES Eigen3::Eigen hana)

# Optional modules.
if(UNGAR_ENABLE_LOGGING)
list(APPEND UNGAR_LINK_LIBRARIES spdlog::spdlog)
endif()
if(UNGAR_ENABLE_AUTODIFF)
list(APPEND UNGAR_LINK_LIBRARIES CppAD::CppAD CppADCodeGen::CppADCodeGen
${CMAKE_DL_LIBS} finitediff::finitediff)
endif()
if(UNGAR_ENABLE_OPTIMIZATION)
list(APPEND UNGAR_LINK_LIBRARIES osqp-cpp::osqp-cpp)
endif()

target_include_directories(ungar INTERFACE ${UNGAR_INCLUDE_DIRECTORIES})
target_link_libraries(ungar INTERFACE ${UNGAR_LINK_LIBRARIES})

# ##############################################################################
# Set Ungar's compile definitions.
# ##############################################################################
target_compile_definitions(
ungar
INTERFACE $<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>
$<$<CONFIG:RelWithDebInfo>:_GLIBCXX_ASSERTIONS>
$<$<CONFIG:MinSizeRel>:UNGAR_CONFIG_ENABLE_RELEASE_MODE>
$<$<CONFIG:Release>:UNGAR_CONFIG_ENABLE_RELEASE_MODE>)

if(UNGAR_ENABLE_LOGGING)
target_compile_definitions(ungar INTERFACE -DUNGAR_CONFIG_ENABLE_LOGGING)
endif()

if(UNGAR_ENABLE_AUTODIFF)
target_compile_definitions(
ungar
INTERFACE -DUNGAR_CONFIG_ENABLE_AUTODIFF
INTERFACE "UNGAR_CODEGEN_FOLDER=\"${CMAKE_BINARY_DIR}/ungar_codegen\"")
endif()

if(UNGAR_ENABLE_OPTIMIZATION)
target_compile_definitions(ungar INTERFACE -DUNGAR_CONFIG_ENABLE_OPTIMIZATION)
endif()

# ##############################################################################
# Add optional targets.
# ##############################################################################
if(UNGAR_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()

if(UNGAR_BUILD_EXAMPLES)
add_subdirectory(example)
endif()

if(UNGAR_INSTALL)
include(GNUInstallDirs)

# Install Ungar headers.
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# Install Ungar binaries.
install(
TARGETS ungar
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# Install Ungar configuration files.
include(CMakePackageConfigHelpers)

configure_package_config_file(
UngarConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ungar)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ungar)

# Perform installation.
install(
EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ungar::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ungar)
endif(UNGAR_INSTALL)
Loading

0 comments on commit cd0040a

Please sign in to comment.