1+ # Copyright (c) The DART development contributors
2+ # All rights reserved.
3+ #
4+ # The list of contributors can be found at:
5+ # https://github.com/dartsim/dart/blob/master/LICENSE
6+ #
7+ # This file is provided under the following "BSD-style" License:
8+ # Redistribution and use in source and binary forms, with or
9+ # without modification, are permitted provided that the following
10+ # conditions are met:
11+ # * Redistributions of source code must retain the above copyright
12+ # notice, this list of conditions and the following disclaimer.
13+ # * Redistributions in binary form must reproduce the above
14+ # copyright notice, this list of conditions and the following
15+ # disclaimer in the documentation and/or other materials provided
16+ # with the distribution.
17+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18+ # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19+ # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25+ # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26+ # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28+ # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+ # POSSIBILITY OF SUCH DAMAGE.
30+
31+ # dart_get_max(var [value1 value2...])
32+ function (dart_get_max var)
33+ set (first YES )
34+ set (choice NO )
35+ foreach (item ${ARGN} )
36+ if (first)
37+ set (choice ${item} )
38+ set (first NO )
39+ elseif (choice LESS ${item} )
40+ set (choice ${item} )
41+ endif ()
42+ endforeach (item)
43+ set (${var} ${choice} PARENT_SCOPE)
44+ endfunction ()
45+
46+ # dart_get_max_string_length(var [value1 value2...])
47+ function (dart_get_max_string_length var)
48+ foreach (item ${ARGN} )
49+ string (LENGTH ${item} length )
50+ list (APPEND list ${length} )
51+ endforeach ()
52+ dart_get_max(choice ${list} )
53+ set (${var} ${choice} PARENT_SCOPE)
54+ endfunction ()
55+
56+ # cmake-format: off
57+ # dart_option(<variable> "<help_text>" <value>)
58+ # cmake-format: on
59+ function (dart_option variable help_text default_value)
60+ set_property (
61+ GLOBAL PROPERTY DART_DETAIL_PROPERTY_OPTION_VARIABLE "${variable} " APPEND
62+ )
63+ set_property (
64+ GLOBAL PROPERTY DART_DETAIL_property_option_help_text "${help_text} " APPEND
65+ )
66+ set_property (
67+ GLOBAL PROPERTY DART_DETAIL_property_option_default_value "${default_value} "
68+ APPEND
69+ )
70+
71+ # Add option
72+ option (${variable} ${help_text} ${default_value} )
73+
74+ # Normalize boolean value variants (e.g. 1/0, On/Off, TRUE/FALSE) to ON/OFF
75+ if (${variable} )
76+ set (${variable} ON PARENT_SCOPE)
77+ else ()
78+ set (${variable} OFF PARENT_SCOPE)
79+ endif ()
80+
81+ endfunction ()
82+
83+ # cmake-format: off
84+ # dart_print_options()
85+ # cmake-format: on
86+ function (dart_print_options)
87+ # Print the header
88+ message (STATUS "[ Options ]" )
89+
90+ get_property (
91+ option_variables GLOBAL PROPERTY DART_DETAIL_PROPERTY_OPTION_VARIABLE
92+ )
93+ get_property (
94+ option_help_texts GLOBAL PROPERTY DART_DETAIL_property_option_help_text
95+ )
96+ get_property (
97+ option_default_values GLOBAL
98+ PROPERTY DART_DETAIL_property_option_default_value
99+ )
100+
101+ dart_get_max_string_length(option_variable_max_len ${option_variables} )
102+ list (LENGTH option_variables option_count)
103+ math (EXPR option_count "${option_count} - 1" )
104+ foreach (val RANGE ${option_count} )
105+ list (GET option_variables ${val} option_variable)
106+ list (GET option_default_values ${val} option_default_value)
107+
108+ set (option_str "- ${option_variable} " )
109+ set (spaces "" )
110+ string (LENGTH ${option_variable} option_variable_len)
111+ math (EXPR space_count "${option_variable_max_len} - ${option_variable_len} " )
112+ foreach (loop_var RANGE ${space_count} )
113+ set (option_str "${option_str} " )
114+ endforeach ()
115+
116+ set (option_str "${option_str} : ${${option_variable} }" )
117+
118+ if (${option_variable} STREQUAL option_default_value)
119+ set (option_str "${option_str} [default]" )
120+ endif ()
121+
122+ message (STATUS "${option_str} " )
123+ endforeach ()
124+
125+ message (STATUS "" )
126+ endfunction ()
127+
128+ function (dart_library)
129+ set (prefix _ARG)
130+ set (options
131+ GLOB_HEADERS
132+ GLOB_SOURCES
133+ )
134+ set (oneValueArgs
135+ NAME
136+ )
137+ set (multiValueArgs
138+ HEADERS
139+ SOURCES
140+ PUBLIC_LINK_LIBRARIES
141+ PRIVATE_LINK_LIBRARIES
142+ PUBLIC_COMPILE_DEFINITIONS
143+ PRIVATE_COMPILE_DEFINITIONS
144+ )
145+ cmake_parse_arguments (
146+ "${prefix} " "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN}
147+ )
148+
149+ # Get the current directory relative to the root of the project
150+ # assuming that dart/* is the root of the source tree
151+ string (REPLACE "${CMAKE_SOURCE_DIR} /" "" relative_path ${CMAKE_CURRENT_SOURCE_DIR} )
152+
153+ if (${_ARG_GLOB_HEADERS} )
154+ file (GLOB_RECURSE headers "*.hpp" )
155+ list (APPEND _ARG_HEADERS ${headers} )
156+ endif ()
157+
158+ if (${_ARG_GLOB_SOURCES} )
159+ file (GLOB_RECURSE sources "*.cpp" )
160+ list (APPEND _ARG_SOURCES ${sources} )
161+ endif ()
162+
163+ add_library (${_ARG_NAME}
164+ ${_ARG_HEADERS}
165+ ${_ARG_SOURCES}
166+ )
167+
168+ target_include_directories (${_ARG_NAME} PUBLIC
169+ $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR} >
170+ $<BUILD_INTERFACE:${CMAKE_BINARY_DIR} >
171+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
172+ )
173+
174+ target_compile_features (${_ARG_NAME} PUBLIC cxx_std_17)
175+
176+ target_link_libraries (${_ARG_NAME}
177+ PUBLIC
178+ ${_ARG_PUBLIC_LINK_LIBRARIES}
179+ PRIVATE
180+ ${_ARG_PRIVATE_LINK_LIBRARIES}
181+ )
182+
183+ target_compile_definitions (${_ARG_NAME}
184+ PUBLIC
185+ ${_ARG_PUBLIC_COMPILE_DEFINITIONS}
186+ PRIVATE
187+ ${_ARG_PRIVATE_COMPILE_DEFINITIONS}
188+ )
189+
190+ set_target_properties (${_ARG_NAME} PROPERTIES
191+ OUTPUT_NAME dart-${_ARG_NAME}
192+ )
193+
194+ include (GNUInstallDirs)
195+
196+ foreach (header ${_ARG_HEADERS} )
197+ # Compute the relative path of each header from the root_dir
198+ file (RELATIVE_PATH rel_path "${CMAKE_SOURCE_DIR} " "${header} " )
199+ get_filename_component (rel_dir "${rel_path} " DIRECTORY )
200+
201+ # Install the file to the destination, preserving the directory structure
202+ install (
203+ FILES "${header} "
204+ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} /${rel_dir} "
205+ )
206+ endforeach ()
207+
208+ install (
209+ TARGETS ${_ARG_NAME}
210+ EXPORT dart_${_ARG_NAME} Targets
211+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
212+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
213+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
214+ )
215+
216+ install (
217+ EXPORT dart_${_ARG_NAME} Targets
218+ NAMESPACE DART::
219+ DESTINATION ${CMAKE_INSTALL_DATAROOTDIR} /${PROJECT_NAME} /cmake
220+ )
221+
222+ dart_format_add(${_ARG_HEADERS} ${_ARG_SOURCES} )
223+
224+ endfunction ()
0 commit comments