blob: ab0eac63336ca77bbe4b0d4da428d2da9f0a25e9 [file] [log] [blame]
Christian Blichmann11bb2c72021-02-04 03:06:44 -08001# 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 Blichmannd4514782022-01-28 01:38:27 -08007# https://www.apache.org/licenses/LICENSE-2.0
Christian Blichmann11bb2c72021-02-04 03:06:44 -08008#
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 Blichmann41e0ca02022-01-25 13:48:07 +010015FetchContent_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 Blichmann11bb2c72021-02-04 03:06:44 -080018)
Christian Blichmann41e0ca02022-01-25 13:48:07 +010019FetchContent_MakeAvailable(libcap)
Christian Blichmann11bb2c72021-02-04 03:06:44 -080020
Christian Blichmann41e0ca02022-01-25 13:48:07 +010021set(libcap_INCLUDE_DIR "${libcap_SOURCE_DIR}/libcap/include")
Christian Blichmann11bb2c72021-02-04 03:06:44 -080022
Christian Blichmann41e0ca02022-01-25 13:48:07 +010023add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/cap_names.list.h
Christian Blichmann11bb2c72021-02-04 03:06:44 -080024 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 Blichmann41e0ca02022-01-25 13:48:07 +010027 ${libcap_SOURCE_DIR}/libcap/include/uapi/linux/capability.h |
28 tr [:upper:] [:lower:] > ${libcap_SOURCE_DIR}/libcap/cap_names.list.h
Christian Blichmann11bb2c72021-02-04 03:06:44 -080029)
30
31if (CMAKE_CROSSCOMPILING AND BUILD_C_COMPILER)
Christian Blichmann41e0ca02022-01-25 13:48:07 +010032 add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/libcap_makenames
Christian Blichmann11bb2c72021-02-04 03:06:44 -080033 VERBATIM
34 # Use the same logic as libcap/Makefile
35 COMMAND ${BUILD_C_COMPILER} ${BUILD_C_FLAGS}
Christian Blichmann41e0ca02022-01-25 13:48:07 +010036 ${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 Blichmann11bb2c72021-02-04 03:06:44 -080040 )
41
Christian Blichmann41e0ca02022-01-25 13:48:07 +010042 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 Blichmann11bb2c72021-02-04 03:06:44 -080046 )
47else()
48 add_executable(libcap_makenames
Christian Blichmann41e0ca02022-01-25 13:48:07 +010049 ${libcap_SOURCE_DIR}/libcap/cap_names.list.h
50 ${libcap_SOURCE_DIR}/libcap/_makenames.c
Christian Blichmann11bb2c72021-02-04 03:06:44 -080051 )
52
53 target_include_directories(libcap_makenames PUBLIC
Christian Blichmann41e0ca02022-01-25 13:48:07 +010054 ${libcap_SOURCE_DIR}/libcap
55 ${libcap_SOURCE_DIR}/libcap/include
56 ${libcap_SOURCE_DIR}/libcap/include/uapi
Christian Blichmann11bb2c72021-02-04 03:06:44 -080057 )
58
Christian Blichmann41e0ca02022-01-25 13:48:07 +010059 add_custom_command(OUTPUT ${libcap_SOURCE_DIR}/libcap/cap_names.h
60 COMMAND libcap_makenames > ${libcap_SOURCE_DIR}/libcap/cap_names.h
Christian Blichmann11bb2c72021-02-04 03:06:44 -080061 )
62endif()
63
64add_library(cap STATIC
Christian Blichmann41e0ca02022-01-25 13:48:07 +010065 ${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 Blichmann11bb2c72021-02-04 03:06:44 -080074)
75add_library(libcap::libcap ALIAS cap)
76target_include_directories(cap PUBLIC
Christian Blichmann41e0ca02022-01-25 13:48:07 +010077 ${libcap_SOURCE_DIR}/libcap
78 ${libcap_SOURCE_DIR}/libcap/include
79 ${libcap_SOURCE_DIR}/libcap/include/uapi
Christian Blichmann11bb2c72021-02-04 03:06:44 -080080)
81target_compile_options(cap PRIVATE
82 -Wno-tautological-compare
83 -Wno-unused-result
84)
85target_compile_definitions(cap PRIVATE
86 # Work around sys/xattr.h not declaring this
87 -DXATTR_NAME_CAPS="\"security.capability\""
88)
89target_link_libraries(cap PRIVATE
90 sapi::base
91)