Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 1 | # Copyright 2019 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
Christian Blichmann | d451478 | 2022-01-28 01:38:27 -0800 | [diff] [blame] | 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 15 | FetchContent_Declare(libcap |
| 16 | URL https://www.kernel.org/pub/linux/libs/security/linux-privs/libcap2/libcap-2.27.tar.gz |
| 17 | URL_HASH SHA256=260b549c154b07c3cdc16b9ccc93c04633c39f4fb6a4a3b8d1fa5b8a9c3f5fe8 |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 18 | ) |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 19 | FetchContent_MakeAvailable(libcap) |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 20 | |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 21 | set(libcap_INCLUDE_DIR "${libcap_SOURCE_DIR}/libcap/include") |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 22 | |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 23 | add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/cap_names.list.h |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 24 | VERBATIM |
| 25 | COMMAND # Use the same logic as libcap/Makefile |
| 26 | sed -ne [=[/^#define[ \\t]CAP[_A-Z]\+[ \\t]\+[0-9]\+/{s/^#define \([^ \\t]*\)[ \\t]*\([^ \\t]*\)/\{\"\1\",\2\},/p;}]=] |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 27 | ${libcap_SOURCE_DIR}/libcap/include/uapi/linux/capability.h | |
| 28 | tr [:upper:] [:lower:] > ${libcap_SOURCE_DIR}/libcap/cap_names.list.h |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | if (CMAKE_CROSSCOMPILING AND BUILD_C_COMPILER) |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 32 | add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/libcap_makenames |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 33 | VERBATIM |
| 34 | # Use the same logic as libcap/Makefile |
| 35 | COMMAND ${BUILD_C_COMPILER} ${BUILD_C_FLAGS} |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 36 | ${libcap_SOURCE_DIR}/libcap/_makenames.c |
| 37 | -o ${libcap_SOURCE_DIR}/libcap/libcap_makenames |
| 38 | DEPENDS ${libcap_SOURCE_DIR}/libcap/cap_names.list.h |
| 39 | ${libcap_SOURCE_DIR}/libcap/_makenames.c |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 40 | ) |
| 41 | |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 42 | add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/cap_names.h |
| 43 | COMMAND ${libcap_SOURCE_DIR}/libcap/libcap_makenames > |
| 44 | ${libcap_SOURCE_DIR}/libcap/cap_names.h |
| 45 | DEPENDS ${libcap_SOURCE_DIR}/libcap/libcap_makenames |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 46 | ) |
| 47 | else() |
| 48 | add_executable(libcap_makenames |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 49 | ${libcap_SOURCE_DIR}/libcap/cap_names.list.h |
| 50 | ${libcap_SOURCE_DIR}/libcap/_makenames.c |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | target_include_directories(libcap_makenames PUBLIC |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 54 | ${libcap_SOURCE_DIR}/libcap |
| 55 | ${libcap_SOURCE_DIR}/libcap/include |
| 56 | ${libcap_SOURCE_DIR}/libcap/include/uapi |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 57 | ) |
| 58 | |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 59 | add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/cap_names.h |
| 60 | COMMAND libcap_makenames > ${libcap_SOURCE_DIR}/libcap/cap_names.h |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 61 | ) |
| 62 | endif() |
| 63 | |
| 64 | add_library(cap STATIC |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 65 | ${libcap_SOURCE_DIR}/libcap/cap_alloc.c |
| 66 | ${libcap_SOURCE_DIR}/libcap/cap_extint.c |
| 67 | ${libcap_SOURCE_DIR}/libcap/cap_file.c |
| 68 | ${libcap_SOURCE_DIR}/libcap/cap_flag.c |
| 69 | ${libcap_SOURCE_DIR}/libcap/cap_names.h |
| 70 | ${libcap_SOURCE_DIR}/libcap/cap_proc.c |
| 71 | ${libcap_SOURCE_DIR}/libcap/cap_text.c |
| 72 | ${libcap_SOURCE_DIR}/libcap/include/uapi/linux/capability.h |
| 73 | ${libcap_SOURCE_DIR}/libcap/libcap.h |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 74 | ) |
| 75 | add_library(libcap::libcap ALIAS cap) |
| 76 | target_include_directories(cap PUBLIC |
Christian Blichmann | 41e0ca0 | 2022-01-25 13:48:07 +0100 | [diff] [blame] | 77 | ${libcap_SOURCE_DIR}/libcap |
| 78 | ${libcap_SOURCE_DIR}/libcap/include |
| 79 | ${libcap_SOURCE_DIR}/libcap/include/uapi |
Christian Blichmann | 11bb2c7 | 2021-02-04 03:06:44 -0800 | [diff] [blame] | 80 | ) |
| 81 | target_compile_options(cap PRIVATE |
| 82 | -Wno-tautological-compare |
| 83 | -Wno-unused-result |
| 84 | ) |
| 85 | target_compile_definitions(cap PRIVATE |
| 86 | # Work around sys/xattr.h not declaring this |
| 87 | -DXATTR_NAME_CAPS="\"security.capability\"" |
| 88 | ) |
| 89 | target_link_libraries(cap PRIVATE |
| 90 | sapi::base |
| 91 | ) |