1set(LIBC_INCLUDE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 2set(LIBC_INCLUDE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 3 4include(LLVMLibCHeaderRules) 5 6# The GPU build wants to install files in the compiler's resource directory. 7if(LIBC_TARGET_OS_IS_GPU) 8 include(GetClangResourceDir) 9endif() 10 11add_subdirectory(llvm-libc-macros) 12add_subdirectory(llvm-libc-types) 13 14add_header( 15 llvm_libc_common_h 16 HDR 17 __llvm-libc-common.h 18) 19 20# TODO: Can we simplify this macro expansion? 21# https://github.com/llvm/llvm-project/issues/117254 22macro(add_header_macro TARGET_NAME YAML_FILE GEN_HDR DEPENDS) 23 add_gen_header( 24 ${TARGET_NAME} 25 YAML_FILE ${YAML_FILE} 26 GEN_HDR ${GEN_HDR} 27 ${DEPENDS} 28 ${ARGN} 29 ) 30endmacro() 31 32add_header_macro( 33 ctype 34 ../libc/include/ctype.yaml 35 ctype.h 36 DEPENDS 37 .llvm_libc_common_h 38 .llvm-libc-types.locale_t 39) 40 41add_header_macro( 42 dirent 43 ../libc/include/dirent.yaml 44 dirent.h 45 DEPENDS 46 .llvm_libc_common_h 47 .llvm-libc-types.ino_t 48 .llvm-libc-types.DIR 49 .llvm-libc-types.struct_dirent 50) 51 52add_header_macro( 53 fcntl 54 ../libc/include/fcntl.yaml 55 fcntl.h 56 DEPENDS 57 .llvm-libc-macros.fcntl_macros 58 .llvm-libc-types.mode_t 59 .llvm-libc-types.struct_flock 60 .llvm-libc-types.struct_flock64 61 .llvm-libc-types.off64_t 62 .llvm-libc-types.pid_t 63 .llvm-libc-types.off_t 64 .llvm_libc_common_h 65) 66 67add_header_macro( 68 dlfcn 69 ../libc/include/dlfcn.yaml 70 dlfcn.h 71 DEPENDS 72 .llvm-libc-macros.dlfcn_macros 73 .llvm_libc_common_h 74) 75 76add_header_macro( 77 features 78 ../libc/include/features.yaml 79 features.h 80 DEPENDS 81 .llvm_libc_common_h 82 .llvm-libc-macros.features_macros 83) 84 85add_header_macro( 86 fenv 87 ../libc/include/fenv.yaml 88 fenv.h 89 DEPENDS 90 .llvm_libc_common_h 91 .llvm-libc-macros.fenv_macros 92 .llvm-libc-types.fenv_t 93 .llvm-libc-types.fexcept_t 94) 95 96add_header_macro( 97 inttypes 98 ../libc/include/inttypes.yaml 99 inttypes.h 100 DEPENDS 101 .llvm_libc_common_h 102 .llvm-libc-types.imaxdiv_t 103 .llvm-libc-macros.inttypes_macros 104) 105 106add_header_macro( 107 float 108 ../libc/include/float.yaml 109 float.h 110 DEPENDS 111 .llvm-libc-macros.float_macros 112) 113 114add_header_macro( 115 stdint 116 ../libc/include/stdint.yaml 117 stdint.h 118 DEPENDS 119 .llvm-libc-macros.stdint_macros 120) 121 122add_header_macro( 123 limits 124 ../libc/include/limits.yaml 125 limits.h 126 DEPENDS 127 .llvm-libc-macros.limits_macros 128) 129 130add_header_macro( 131 malloc 132 ../libc/include/malloc.yaml 133 malloc.h 134 DEPENDS 135 .llvm_libc_common_h 136 .llvm-libc-macros.malloc_macros 137) 138 139add_header_macro( 140 math 141 ../libc/include/math.yaml 142 math.h 143 DEPENDS 144 .llvm_libc_common_h 145 .llvm-libc-macros.float16_macros 146 .llvm-libc-macros.math_macros 147 .llvm-libc-macros.math_function_macros 148 .llvm-libc-types.double_t 149 .llvm-libc-types.float_t 150 .llvm-libc-types.float128 151) 152 153add_header_macro( 154 stdfix 155 ../libc/include/stdfix.yaml 156 stdfix.h 157 DEPENDS 158 .llvm-libc-macros.stdfix_macros 159 .llvm-libc-types.stdfix-types 160) 161 162# TODO: This should be conditional on POSIX networking being included. 163file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/arpa) 164 165add_header_macro( 166 arpa_inet 167 ../libc/include/arpa/inet.yaml 168 arpa/inet.h 169 DEPENDS 170 .llvm_libc_common_h 171) 172 173add_header_macro( 174 assert 175 ../libc/include/assert.yaml 176 assert.h 177 DEPENDS 178 .llvm_libc_common_h 179 .llvm-libc-macros.assert_macros 180) 181 182add_header_macro( 183 complex 184 ../libc/include/complex.yaml 185 complex.h 186 DEPENDS 187 .llvm_libc_common_h 188 .llvm-libc-macros.complex_macros 189) 190 191add_header_macro( 192 setjmp 193 ../libc/include/setjmp.yaml 194 setjmp.h 195 DEPENDS 196 .llvm_libc_common_h 197 .llvm-libc-types.jmp_buf 198) 199 200add_header_macro( 201 string 202 ../libc/include/string.yaml 203 string.h 204 DEPENDS 205 .llvm_libc_common_h 206 .llvm-libc-macros.null_macro 207 .llvm-libc-types.size_t 208) 209 210add_header_macro( 211 strings 212 ../libc/include/strings.yaml 213 strings.h 214 DEPENDS 215 .llvm_libc_common_h 216 .llvm-libc-types.size_t 217) 218 219add_header_macro( 220 search 221 ../libc/include/search.yaml 222 search.h 223 DEPENDS 224 .llvm_libc_common_h 225 .llvm-libc-types.ACTION 226 .llvm-libc-types.ENTRY 227 .llvm-libc-types.struct_hsearch_data 228 .llvm-libc-types.size_t 229 .llvm-libc-types.__lsearchcompare_t 230) 231 232add_header_macro( 233 time 234 ../libc/include/time.yaml 235 time.h 236 DEPENDS 237 .llvm_libc_common_h 238 .llvm-libc-macros.time_macros 239 .llvm-libc-types.clock_t 240 .llvm-libc-types.time_t 241 .llvm-libc-types.struct_tm 242 .llvm-libc-types.struct_timespec 243 .llvm-libc-types.struct_timeval 244 .llvm-libc-types.clockid_t 245) 246 247add_header_macro( 248 threads 249 ../libc/include/threads.yaml 250 threads.h 251 DEPENDS 252 .llvm_libc_common_h 253 .llvm-libc-types.__call_once_func_t 254 .llvm-libc-types.once_flag 255 .llvm-libc-types.cnd_t 256 .llvm-libc-types.mtx_t 257 .llvm-libc-types.thrd_t 258 .llvm-libc-types.thrd_start_t 259 .llvm-libc-types.tss_t 260 .llvm-libc-types.tss_dtor_t 261) 262 263add_header_macro( 264 errno 265 ../libc/include/errno.yaml 266 errno.h 267 DEPENDS 268 .llvm-libc-macros.generic_error_number_macros 269 .llvm-libc-macros.error_number_macros 270) 271 272add_header_macro( 273 signal 274 ../libc/include/signal.yaml 275 signal.h 276 DEPENDS 277 .llvm-libc-macros.signal_macros 278 .llvm-libc-types.sig_atomic_t 279 .llvm-libc-types.sigset_t 280 .llvm-libc-types.struct_sigaction 281 .llvm-libc-types.union_sigval 282 .llvm-libc-types.siginfo_t 283 .llvm-libc-types.stack_t 284 .llvm-libc-types.pid_t 285) 286 287add_header_macro( 288 stdbit 289 ../libc/include/stdbit.yaml 290 stdbit.h 291 DEPENDS 292 .llvm_libc_common_h 293 .llvm-libc-macros.stdbit_macros 294) 295 296add_header_macro( 297 stdckdint 298 ../libc/include/stdckdint.yaml 299 stdckdint.h 300 DEPENDS 301 .llvm_libc_common_h 302 .llvm-libc-macros.stdckdint_macros 303) 304 305add_header_macro( 306 stdio 307 ../libc/include/stdio.yaml 308 stdio.h 309 DEPENDS 310 .llvm-libc-macros.file_seek_macros 311 .llvm-libc-macros.stdio_macros 312 .llvm-libc-types.FILE 313 .llvm-libc-types.cookie_io_functions_t 314 .llvm-libc-types.off_t 315 .llvm-libc-types.size_t 316 .llvm-libc-types.ssize_t 317 .llvm_libc_common_h 318) 319 320add_header_macro( 321 stdlib 322 ../libc/include/stdlib.yaml 323 stdlib.h 324 DEPENDS 325 .llvm_libc_common_h 326 .llvm-libc-macros.stdlib_macros 327 .llvm-libc-types.div_t 328 .llvm-libc-types.ldiv_t 329 .llvm-libc-types.lldiv_t 330 .llvm-libc-types.size_t 331 .llvm-libc-types.__bsearchcompare_t 332 .llvm-libc-types.__qsortcompare_t 333 .llvm-libc-types.__qsortrcompare_t 334 .llvm-libc-types.__atexithandler_t 335 .llvm-libc-types.locale_t 336) 337 338add_header_macro( 339 unistd 340 ../libc/include/unistd.yaml 341 unistd.h 342 DEPENDS 343 .llvm_libc_common_h 344 .llvm-libc-macros.file_seek_macros 345 .llvm-libc-macros.unistd_macros 346 .llvm-libc-types.__exec_argv_t 347 .llvm-libc-types.__exec_envp_t 348 .llvm-libc-types.off_t 349 .llvm-libc-types.pid_t 350 .llvm-libc-types.size_t 351 .llvm-libc-types.ssize_t 352 .llvm-libc-types.uid_t 353 .llvm-libc-types.__getoptargv_t 354) 355 356add_header_macro( 357 pthread 358 ../libc/include/pthread.yaml 359 pthread.h 360 DEPENDS 361 .llvm-libc-macros.pthread_macros 362 .llvm-libc-types.__atfork_callback_t 363 .llvm-libc-types.__pthread_once_func_t 364 .llvm-libc-types.__pthread_start_t 365 .llvm-libc-types.__pthread_tss_dtor_t 366 .llvm-libc-types.pthread_attr_t 367 .llvm-libc-types.pthread_condattr_t 368 .llvm-libc-types.pthread_key_t 369 .llvm-libc-types.pthread_mutex_t 370 .llvm-libc-types.pthread_mutexattr_t 371 .llvm-libc-types.pthread_once_t 372 .llvm-libc-types.pthread_rwlock_t 373 .llvm-libc-types.pthread_rwlockattr_t 374 .llvm-libc-types.pthread_spinlock_t 375 .llvm-libc-types.pthread_t 376 .llvm_libc_common_h 377) 378 379add_header_macro( 380 sched 381 ../libc/include/sched.yaml 382 sched.h 383 DEPENDS 384 .llvm_libc_common_h 385 .llvm-libc-macros.sched_macros 386 .llvm-libc-types.cpu_set_t 387 .llvm-libc-types.pid_t 388 .llvm-libc-types.size_t 389 .llvm-libc-types.struct_sched_param 390 # Needed according to posix standard 391 .llvm-libc-types.time_t 392 .llvm-libc-types.struct_timespec 393) 394 395add_header_macro( 396 spawn 397 ../libc/include/spawn.yaml 398 spawn.h 399 DEPENDS 400 .llvm_libc_common_h 401 .llvm-libc-types.mode_t 402 .llvm-libc-types.pid_t 403 .llvm-libc-types.posix_spawnattr_t 404 .llvm-libc-types.posix_spawn_file_actions_t 405) 406 407add_header_macro( 408 link 409 ../libc/include/link.yaml 410 link.h 411 DEPENDS 412 .llvm_libc_common_h 413 .llvm-libc-macros.link_macros 414) 415 416add_header_macro( 417 elf 418 ../libc/include/elf.yaml 419 elf.h 420 DEPENDS 421 .llvm-libc-macros.elf_macros 422) 423 424# TODO: Not all platforms will have a include/sys directory. Add the sys 425# directory and the targets for sys/*.h files conditional to the OS requiring 426# them. 427file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/sys) 428 429add_header_macro( 430 sys_auxv 431 ../libc/include/sys/auxv.yaml 432 sys/auxv.h 433 DEPENDS 434 .llvm_libc_common_h 435 .llvm-libc-macros.sys_auxv_macros 436) 437 438add_header_macro( 439 sys_epoll 440 ../libc/include/sys/epoll.yaml 441 sys/epoll.h 442 DEPENDS 443 .llvm_libc_common_h 444 .llvm-libc-types.struct_epoll_event 445 .llvm-libc-types.struct_epoll_data 446 .llvm-libc-types.sigset_t 447 .llvm-libc-macros.sys_epoll_macros 448) 449 450add_header_macro( 451 sys_ioctl 452 ../libc/include/sys/ioctl.yaml 453 sys/ioctl.h 454 DEPENDS 455 .llvm_libc_common_h 456 .llvm-libc-macros.sys_ioctl_macros 457) 458 459add_header_macro( 460 sys_mman 461 ../libc/include/sys/mman.yaml 462 sys/mman.h 463 DEPENDS 464 .llvm_libc_common_h 465 .llvm-libc-macros.sys_mman_macros 466 .llvm-libc-types.off_t 467 .llvm-libc-types.size_t 468 .llvm-libc-types.ssize_t 469) 470 471add_header_macro( 472 sys_prctl 473 ../libc/include/sys/prctl.yaml 474 sys/prctl.h 475 DEPENDS 476 .llvm_libc_common_h 477) 478 479add_header( 480 sys_queue 481 HDR 482 sys/queue.h 483 DEPENDS 484 .llvm-libc-macros.sys_queue_macros 485) 486 487add_header_macro( 488 sys_random 489 ../libc/include/sys/random.yaml 490 sys/random.h 491 DEPENDS 492 .llvm_libc_common_h 493 .llvm-libc-macros.sys_random_macros 494 .llvm-libc-types.size_t 495 .llvm-libc-types.ssize_t 496) 497 498add_header_macro( 499 sys_resource 500 ../libc/include/sys/resource.yaml 501 sys/resource.h 502 DEPENDS 503 .llvm_libc_common_h 504 .llvm-libc-macros.sys_resource_macros 505 .llvm-libc-types.rlim_t 506 .llvm-libc-types.struct_rlimit 507) 508 509add_header_macro( 510 sys_stat 511 ../libc/include/sys/stat.yaml 512 sys/stat.h 513 DEPENDS 514 .llvm_libc_common_h 515 .llvm-libc-macros.sys_stat_macros 516 .llvm-libc-types.mode_t 517 .llvm-libc-types.dev_t 518 .llvm-libc-types.ino_t 519 .llvm-libc-types.nlink_t 520 .llvm-libc-types.uid_t 521 .llvm-libc-types.gid_t 522 .llvm-libc-types.off_t 523 .llvm-libc-types.struct_timespec 524 .llvm-libc-types.struct_timeval 525 .llvm-libc-types.blksize_t 526 .llvm-libc-types.blkcnt_t 527 .llvm-libc-types.struct_stat 528) 529 530add_header_macro( 531 sys_select 532 ../libc/include/sys/select.yaml 533 sys/select.h 534 DEPENDS 535 .llvm_libc_common_h 536 .llvm-libc-macros.sys_select_macros 537 .llvm-libc-types.fd_set 538 .llvm-libc-types.sigset_t 539 .llvm-libc-types.suseconds_t 540 .llvm-libc-types.time_t 541 .llvm-libc-types.struct_timespec 542 .llvm-libc-types.struct_timeval 543) 544 545add_header_macro( 546 sys_sendfile 547 ../libc/include/sys/sendfile.yaml 548 sys/sendfile.h 549 DEPENDS 550 .llvm_libc_common_h 551 .llvm-libc-types.off_t 552 .llvm-libc-types.size_t 553 .llvm-libc-types.ssize_t 554) 555 556add_header_macro( 557 sys_socket 558 ../libc/include/sys/socket.yaml 559 sys/socket.h 560 DEPENDS 561 .llvm_libc_common_h 562 .llvm-libc-macros.sys_socket_macros 563 .llvm-libc-types.sa_family_t 564 .llvm-libc-types.socklen_t 565 .llvm-libc-types.struct_iovec 566 .llvm-libc-types.struct_msghdr 567 .llvm-libc-types.struct_sockaddr 568 .llvm-libc-types.struct_sockaddr_un 569) 570 571add_header_macro( 572 sys_statvfs 573 ../libc/include/sys/statvfs.yaml 574 sys/statvfs.h 575 DEPENDS 576 .llvm_libc_common_h 577 .llvm-libc-types.struct_statvfs 578) 579 580add_header_macro( 581 sys_syscall 582 ../libc/include/sys/syscall.yaml 583 sys/syscall.h 584 DEPENDS 585) 586 587add_header_macro( 588 sys_time 589 ../libc/include/sys/time.yaml 590 sys/time.h 591 DEPENDS 592 .llvm_libc_common_h 593 .llvm-libc-types.struct_timeval 594 .llvm-libc-macros.sys_time_macros 595) 596 597add_header_macro( 598 sys_types 599 ../libc/include/sys/types.yaml 600 sys/types.h 601 DEPENDS 602 .llvm_libc_common_h 603 .llvm-libc-types.blkcnt_t 604 .llvm-libc-types.blksize_t 605 .llvm-libc-types.clockid_t 606 .llvm-libc-types.dev_t 607 .llvm-libc-types.gid_t 608 .llvm-libc-types.ino_t 609 .llvm-libc-types.mode_t 610 .llvm-libc-types.nlink_t 611 .llvm-libc-types.off_t 612 .llvm-libc-types.pid_t 613 .llvm-libc-types.pthread_attr_t 614 .llvm-libc-types.pthread_key_t 615 .llvm-libc-types.pthread_mutex_t 616 .llvm-libc-types.pthread_mutexattr_t 617 .llvm-libc-types.pthread_once_t 618 .llvm-libc-types.pthread_t 619 .llvm-libc-types.size_t 620 .llvm-libc-types.ssize_t 621 .llvm-libc-types.suseconds_t 622 .llvm-libc-types.time_t 623 .llvm-libc-types.uid_t 624) 625 626add_header_macro( 627 sys_utsname 628 ../libc/include/sys/utsname.yaml 629 sys/utsname.h 630 DEPENDS 631 .llvm_libc_common_h 632 .llvm-libc-types.struct_utsname 633) 634 635add_header_macro( 636 sys_uio 637 ../libc/include/sys/uio.yaml 638 sys/uio.h 639 DEPENDS 640 .llvm_libc_common_h 641 .llvm-libc-types.struct_iovec 642 .llvm-libc-types.ssize_t 643) 644 645add_header_macro( 646 sys_wait 647 ../libc/include/sys/wait.yaml 648 sys/wait.h 649 DEPENDS 650 .llvm_libc_common_h 651 .llvm-libc-macros.sys_wait_macros 652 .llvm-libc-types.pid_t 653 .llvm-libc-types.struct_rusage 654 .llvm-libc-types.siginfo_t 655) 656 657add_header_macro( 658 termios 659 ../libc/include/termios.yaml 660 termios.h 661 DEPENDS 662 .llvm_libc_common_h 663 .llvm-libc-macros.termios_macros 664 .llvm-libc-types.cc_t 665 .llvm-libc-types.pid_t 666 .llvm-libc-types.speed_t 667 .llvm-libc-types.struct_termios 668 .llvm-libc-types.tcflag_t 669) 670 671add_header_macro( 672 uchar 673 ../libc/include/uchar.yaml 674 uchar.h 675 DEPENDS 676 .llvm_libc_common_h 677 .llvm-libc-types.mbstate_t 678 .llvm-libc-types.char8_t 679 .llvm-libc-types.char16_t 680 .llvm-libc-types.char32_t 681) 682 683add_header_macro( 684 wchar 685 ../libc/include/wchar.yaml 686 wchar.h 687 DEPENDS 688 .llvm_libc_common_h 689 .llvm-libc-macros.wchar_macros 690 .llvm-libc-types.mbstate_t 691 .llvm-libc-types.size_t 692 .llvm-libc-types.wint_t 693 .llvm-libc-types.wchar_t 694) 695 696add_header_macro( 697 locale 698 ../libc/include/locale.yaml 699 locale.h 700 DEPENDS 701 .llvm_libc_common_h 702 .llvm-libc-macros.locale_macros 703 .llvm-libc-types.locale_t 704 .llvm-libc-types.struct_lconv 705) 706 707if(NOT LLVM_LIBC_FULL_BUILD) 708 # We don't install headers in non-fullbuild mode. 709 return() 710endif() 711 712function(get_all_install_header_targets out_var) 713 set(all_deps ${ARGN}) 714 foreach(target IN LISTS ARGN) 715 get_target_property(deps ${target} DEPS) 716 if(NOT deps) 717 continue() 718 endif() 719 list(APPEND all_deps ${deps}) 720 get_all_install_header_targets(nested_deps ${deps}) 721 list(APPEND all_deps ${nested_deps}) 722 endforeach() 723 list(REMOVE_DUPLICATES all_deps) 724 set(${out_var} ${all_deps} PARENT_SCOPE) 725endfunction(get_all_install_header_targets) 726 727get_all_install_header_targets(all_install_header_targets ${TARGET_PUBLIC_HEADERS}) 728add_library(libc-headers INTERFACE) 729add_dependencies(libc-headers ${all_install_header_targets}) 730target_include_directories(libc-headers SYSTEM INTERFACE ${LIBC_INCLUDE_DIR}) 731 732foreach(target IN LISTS all_install_header_targets) 733 get_target_property(header_file ${target} HEADER_FILE_PATH) 734 if(NOT header_file) 735 message(FATAL_ERROR "Installable header file '${target}' does not have the " 736 "HEADER_FILE_PATH property set.") 737 endif() 738 file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_DIR} ${header_file}) 739 get_filename_component(nested_dir ${relative_path} DIRECTORY) 740 install(FILES ${header_file} 741 DESTINATION ${LIBC_INSTALL_INCLUDE_DIR}/${nested_dir} 742 COMPONENT libc-headers) 743 # The GPU optionally provides the supported declarations externally so 744 # offloading languages like CUDA and OpenMP know what is supported by libc. We 745 # install these in the compiler's resource directory at a preset location. 746 if(LIBC_TARGET_OS_IS_GPU AND PACKAGE_VERSION) 747 get_target_property(decls_file ${target} DECLS_FILE_PATH) 748 if(NOT decls_file) 749 continue() 750 endif() 751 get_clang_resource_dir(resource_dir SUBDIR include) 752 file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_DIR} ${decls_file}) 753 get_filename_component(nested_dir ${relative_path} DIRECTORY) 754 set(install_dir 755 ${CMAKE_INSTALL_PREFIX}/${resource_dir}/llvm_libc_wrappers/${nested_dir}) 756 install(FILES ${decls_file} 757 DESTINATION ${install_dir} 758 COMPONENT libc-headers) 759 endif() 760endforeach() 761 762if(LLVM_LIBC_FULL_BUILD) 763 add_custom_target(install-libc-headers 764 DEPENDS libc-headers 765 COMMAND "${CMAKE_COMMAND}" 766 -DCMAKE_INSTALL_COMPONENT=libc-headers 767 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") 768 # Stripping is a no-op for headers 769 add_custom_target(install-libc-headers-stripped DEPENDS install-libc-headers) 770endif() 771