1set(OBJECT_LIBRARY_TARGET_TYPE "OBJECT_LIBRARY") 2 3# Rule which is essentially a wrapper over add_library to compile a set of 4# sources to object files. 5# Usage: 6# add_object_library( 7# <target_name> 8# HDRS <list of header files> 9# SRCS <list of source files> 10# DEPENDS <list of dependencies> 11# COMPILE_OPTIONS <optional list of special compile options for this target> 12function(add_object_library target_name) 13 cmake_parse_arguments( 14 "ADD_OBJECT" 15 "" # No option arguments 16 "" # Single value arguments 17 "SRCS;HDRS;COMPILE_OPTIONS;DEPENDS" # Multivalue arguments 18 ${ARGN} 19 ) 20 21 if(NOT ADD_OBJECT_SRCS) 22 message(FATAL_ERROR "'add_object_library' rule requires SRCS to be specified.") 23 endif() 24 25 get_fq_target_name(${target_name} fq_target_name) 26 add_library( 27 ${fq_target_name} 28 OBJECT 29 ${ADD_OBJECT_SRCS} 30 ${ADD_OBJECT_HDRS} 31 ) 32 target_include_directories( 33 ${fq_target_name} 34 PRIVATE 35 ${LIBC_BUILD_DIR}/include 36 ${LIBC_SOURCE_DIR} 37 ${LIBC_BUILD_DIR} 38 ) 39 if(ADD_OBJECT_COMPILE_OPTIONS) 40 target_compile_options( 41 ${fq_target_name} 42 PRIVATE ${ADD_OBJECT_COMPILE_OPTIONS} 43 ) 44 endif() 45 46 get_fq_deps_list(fq_deps_list ${ADD_OBJECT_DEPENDS}) 47 if(fq_deps_list) 48 add_dependencies(${fq_target_name} ${fq_deps_list}) 49 endif() 50 51 set_target_properties( 52 ${fq_target_name} 53 PROPERTIES 54 "TARGET_TYPE" ${OBJECT_LIBRARY_TARGET_TYPE} 55 "OBJECT_FILES" "$<TARGET_OBJECTS:${fq_target_name}>" 56 "DEPS" "${fq_deps_list}" 57 ) 58endfunction(add_object_library) 59 60set(ENTRYPOINT_OBJ_TARGET_TYPE "ENTRYPOINT_OBJ") 61 62# A rule for entrypoint object targets. 63# Usage: 64# add_entrypoint_object( 65# <target_name> 66# [ALIAS|REDIRECTED] # Specified if the entrypoint is redirected or an alias. 67# [NAME] <the C name of the entrypoint if different from target_name> 68# SRCS <list of .cpp files> 69# HDRS <list of .h files> 70# DEPENDS <list of dependencies> 71# COMPILE_OPTIONS <optional list of special compile options for this target> 72# SPECIAL_OBJECTS <optional list of special object targets added by the rule `add_object`> 73# ) 74function(add_entrypoint_object target_name) 75 cmake_parse_arguments( 76 "ADD_ENTRYPOINT_OBJ" 77 "ALIAS;REDIRECTED" # Optional argument 78 "NAME" # Single value arguments 79 "SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments 80 ${ARGN} 81 ) 82 83 get_fq_target_name(${target_name} fq_target_name) 84 set(entrypoint_name ${target_name}) 85 if(ADD_ENTRYPOINT_OBJ_NAME) 86 set(entrypoint_name ${ADD_ENTRYPOINT_OBJ_NAME}) 87 endif() 88 89 list(FIND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name} entrypoint_name_index) 90 if(${entrypoint_name_index} EQUAL -1) 91 add_custom_target(${fq_target_name}) 92 set_target_properties( 93 ${fq_target_name} 94 PROPERTIES 95 "ENTRYPOINT_NAME" ${entrypoint_name} 96 "TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE} 97 "OBJECT_FILE" "" 98 "OBJECT_FILE_RAW" "" 99 "DEPS" "" 100 "SKIPPED" "YES" 101 ) 102 message(STATUS "Skipping libc entrypoint ${fq_target_name}.") 103 return() 104 endif() 105 106 if(ADD_ENTRYPOINT_OBJ_ALIAS) 107 # Alias targets help one add aliases to other entrypoint object targets. 108 # One can use alias targets setup OS/machine independent entrypoint targets. 109 list(LENGTH ADD_ENTRYPOINT_OBJ_DEPENDS deps_size) 110 if(NOT (${deps_size} EQUAL "1")) 111 message(FATAL_ERROR "An entrypoint alias should have exactly one dependency.") 112 endif() 113 list(GET ADD_ENTRYPOINT_OBJ_DEPENDS 0 dep_target) 114 get_fq_dep_name(fq_dep_name ${dep_target}) 115 if(NOT TARGET ${fq_dep_name}) 116 message(WARNING "Aliasee ${fq_dep_name} for entrypoint alias ${target_name} missing; " 117 "Target ${target_name} will be ignored.") 118 return() 119 endif() 120 121 get_target_property(obj_type ${fq_dep_name} "TARGET_TYPE") 122 if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))) 123 message(FATAL_ERROR "The aliasee of an entrypoint alias should be an entrypoint.") 124 endif() 125 126 add_custom_target(${fq_target_name}) 127 add_dependencies(${fq_target_name} ${fq_dep_name}) 128 get_target_property(object_file ${fq_dep_name} "OBJECT_FILE") 129 get_target_property(object_file_raw ${fq_dep_name} "OBJECT_FILE_RAW") 130 set_target_properties( 131 ${fq_target_name} 132 PROPERTIES 133 "ENTRYPOINT_NAME" ${entrypoint_name} 134 "TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE} 135 "IS_ALIAS" "YES" 136 "OBJECT_FILE" "" 137 "OBJECT_FILE_RAW" "" 138 "DEPS" "${fq_dep_name}" 139 ) 140 return() 141 endif() 142 143 if(NOT ADD_ENTRYPOINT_OBJ_SRCS) 144 message(FATAL_ERROR "`add_entrypoint_object` rule requires SRCS to be specified.") 145 endif() 146 if(NOT ADD_ENTRYPOINT_OBJ_HDRS) 147 message(FATAL_ERROR "`add_entrypoint_object` rule requires HDRS to be specified.") 148 endif() 149 150 set(common_compile_options -fpie ${LLVM_CXX_STD_default} -ffreestanding ${ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS}) 151 set(internal_target_name ${fq_target_name}.__internal__) 152 set(include_dirs ${LIBC_BUILD_DIR}/include ${LIBC_SOURCE_DIR} ${LIBC_BUILD_DIR}) 153 get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS}) 154 set(full_deps_list ${fq_deps_list} libc.src.__support.common) 155 156 add_library( 157 ${internal_target_name} 158 # TODO: We don't need an object library for internal consumption. 159 # A future change should switch this to a normal static library. 160 OBJECT 161 ${ADD_ENTRYPOINT_OBJ_SRCS} 162 ${ADD_ENTRYPOINT_OBJ_HDRS} 163 ) 164 target_compile_options(${internal_target_name} BEFORE PRIVATE ${common_compile_options}) 165 target_include_directories(${internal_target_name} PRIVATE ${include_dirs}) 166 add_dependencies(${internal_target_name} ${full_deps_list}) 167 168 add_library( 169 ${fq_target_name} 170 # We want an object library as the objects will eventually get packaged into 171 # an archive (like libc.a). 172 OBJECT 173 ${ADD_ENTRYPOINT_OBJ_SRCS} 174 ${ADD_ENTRYPOINT_OBJ_HDRS} 175 ) 176 target_compile_options( 177 ${fq_target_name} BEFORE PRIVATE ${common_compile_options} -DLLVM_LIBC_PUBLIC_PACKAGING 178 ) 179 target_include_directories(${fq_target_name} PRIVATE ${include_dirs}) 180 add_dependencies(${fq_target_name} ${full_deps_list}) 181 182 set_target_properties( 183 ${fq_target_name} 184 PROPERTIES 185 "ENTRYPOINT_NAME" ${entrypoint_name} 186 "TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE} 187 "OBJECT_FILE" $<TARGET_OBJECTS:${fq_target_name}> 188 # TODO: We don't need to list internal object files if the internal 189 # target is a normal static library. 190 "OBJECT_FILE_RAW" $<TARGET_OBJECTS:${internal_target_name}> 191 "DEPS" "${fq_deps_list}" 192 ) 193 194 if(LLVM_LIBC_ENABLE_LINTING) 195 196 # We only want a second invocation of clang-tidy to run 197 # restrict-system-libc-headers if the compiler-resource-dir was set in 198 # order to prevent false-positives due to a mismatch between the host 199 # compiler and the compiled clang-tidy. 200 if(COMPILER_RESOURCE_DIR) 201 # We run restrict-system-libc-headers with --system-headers to prevent 202 # transitive inclusion through compler provided headers. 203 set(restrict_system_headers_check_invocation 204 COMMAND $<TARGET_FILE:clang-tidy> --system-headers 205 --checks="-*,llvmlibc-restrict-system-libc-headers" 206 # We explicitly set the resource dir here to match the 207 # resource dir of the host compiler. 208 "--extra-arg=-resource-dir=${COMPILER_RESOURCE_DIR}" 209 --quiet 210 -p ${PROJECT_BINARY_DIR} 211 ${ADD_ENTRYPOINT_OBJ_SRCS} 212 ) 213 else() 214 set(restrict_system_headers_check_invocation 215 COMMAND ${CMAKE_COMMAND} -E echo "Header file check skipped") 216 endif() 217 218 set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__") 219 add_custom_command( 220 OUTPUT ${lint_timestamp} 221 # --quiet is used to surpress warning statistics from clang-tidy like: 222 # Suppressed X warnings (X in non-user code). 223 # There seems to be a bug in clang-tidy where by even with --quiet some 224 # messages from clang's own diagnostics engine leak through: 225 # X warnings generated. 226 # Until this is fixed upstream, we use -fno-caret-diagnostics to surpress 227 # these. 228 COMMAND $<TARGET_FILE:clang-tidy> 229 "--extra-arg=-fno-caret-diagnostics" --quiet 230 # Path to directory containing compile_commands.json 231 -p ${PROJECT_BINARY_DIR} 232 ${ADD_ENTRYPOINT_OBJ_SRCS} 233 # See above: this might be a second invocation of clang-tidy depending on 234 # the conditions above. 235 ${restrict_system_headers_check_invocation} 236 # We have two options for running commands, add_custom_command and 237 # add_custom_target. We don't want to run the linter unless source files 238 # have changed. add_custom_target explicitly runs everytime therefore we 239 # use add_custom_command. This function requires an output file and since 240 # linting doesn't produce a file, we create a dummy file using a 241 # crossplatform touch. 242 COMMAND "${CMAKE_COMMAND}" -E touch ${lint_timestamp} 243 COMMENT "Linting... ${target_name}" 244 DEPENDS clang-tidy ${internal_target_name} ${ADD_ENTRYPOINT_OBJ_SRCS} 245 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 246 ) 247 248 add_custom_target(${fq_target_name}.__lint__ 249 DEPENDS ${lint_timestamp}) 250 add_dependencies(lint-libc ${fq_target_name}.__lint__) 251 add_dependencies(${fq_target_name} ${fq_target_name}.__lint__) 252 endif() 253 254endfunction(add_entrypoint_object) 255 256# Rule build a redirector object file. 257function(add_redirector_object target_name) 258 cmake_parse_arguments( 259 "REDIRECTOR_OBJECT" 260 "" # No optional arguments 261 "SRC" # The cpp file in which the redirector is defined. 262 "" # No multivalue arguments 263 ${ARGN} 264 ) 265 if(NOT REDIRECTOR_OBJECT_SRC) 266 message(FATAL_ERROR "'add_redirector_object' rule requires SRC option listing one source file.") 267 endif() 268 269 add_library( 270 ${target_name} 271 OBJECT 272 ${REDIRECTOR_OBJECT_SRC} 273 ) 274 target_compile_options( 275 ${target_name} 276 BEFORE PRIVATE -fPIC 277 ) 278endfunction(add_redirector_object) 279