Skip to content

Commit f538f1a

Browse files
[libc] warn when depending on public entrypoints (#146163)
Add a cmake warning when an entrypoint or object library depends on a public entrypoint.
1 parent c548c47 commit f538f1a

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
set(OBJECT_LIBRARY_TARGET_TYPE "OBJECT_LIBRARY")
2+
set(ENTRYPOINT_OBJ_TARGET_TYPE "ENTRYPOINT_OBJ")
3+
set(ENTRYPOINT_EXT_TARGET_TYPE "ENTRYPOINT_EXT")
4+
5+
# Rule to check if a list of dependencies contains any entrypoint objects. Returns a list in entrypoint_deps.
6+
function(check_entrypoint_deps entrypoint_deps)
7+
set(PUBLIC_DEPS "")
8+
set(fq_deps_list "")
9+
list(APPEND fq_deps_list ${ARGN})
10+
11+
#don't warn for deps that are allowed, such as errno
12+
set(ALLOWED_DEPS
13+
"libc.src.errno.errno"
14+
"libc.src.setjmp.longjmp"
15+
)
16+
list(REMOVE_ITEM fq_deps_list ${ALLOWED_DEPS})
17+
18+
foreach(dep IN LISTS fq_deps_list)
19+
if(NOT TARGET ${dep})
20+
continue()
21+
endif()
22+
23+
get_target_property(target_type ${dep} "TARGET_TYPE")
24+
if(${target_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
25+
list(APPEND PUBLIC_DEPS ${dep})
26+
endif()
27+
endforeach()
28+
set(${entrypoint_deps} ${PUBLIC_DEPS} PARENT_SCOPE)
29+
endfunction()
30+
231

332
# Rule which is essentially a wrapper over add_library to compile a set of
433
# sources to object files.
@@ -65,6 +94,18 @@ function(create_object_library fq_target_name)
6594
target_include_directories(${fq_target_name} PRIVATE ${LIBC_SOURCE_DIR})
6695
target_compile_options(${fq_target_name} PRIVATE ${compile_options})
6796

97+
#loop through the deps, check if any have the TARGET_TYPE of ENTRYPOINT_OBJ_TARGET_TYPE, and print a warning if they do.
98+
if(LIBC_CMAKE_VERBOSE_LOGGING)
99+
set(entrypoint_deps "")
100+
if(NOT "${fq_deps_list}" STREQUAL "")
101+
check_entrypoint_deps(entrypoint_deps ${fq_deps_list})
102+
endif()
103+
if(NOT "${entrypoint_deps}" STREQUAL "")
104+
message(WARNING "Object ${fq_target_name} depends on public entrypoint(s) ${entrypoint_deps}.
105+
Depending on public entrypoints is not allowed in internal code.")
106+
endif()
107+
endif()
108+
68109
if(SHOW_INTERMEDIATE_OBJECTS)
69110
message(STATUS "Adding object library ${fq_target_name}")
70111
if(${SHOW_INTERMEDIATE_OBJECTS} STREQUAL "DEPS")
@@ -110,7 +151,6 @@ function(add_object_library target_name)
110151
${ARGN})
111152
endfunction(add_object_library)
112153

113-
set(ENTRYPOINT_OBJ_TARGET_TYPE "ENTRYPOINT_OBJ")
114154

115155
# A rule for entrypoint object targets.
116156
# Usage:
@@ -179,7 +219,6 @@ function(create_entrypoint_object fq_target_name)
179219

180220
get_target_property(obj_type ${fq_dep_name} "TARGET_TYPE")
181221
if((NOT obj_type) OR (NOT ${obj_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
182-
183222
message(FATAL_ERROR "The aliasee of an entrypoint alias should be an entrypoint.")
184223
endif()
185224

@@ -230,6 +269,19 @@ function(create_entrypoint_object fq_target_name)
230269
_get_common_compile_options(common_compile_options "${ADD_ENTRYPOINT_OBJ_FLAGS}")
231270
list(APPEND common_compile_options ${ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS})
232271
get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS})
272+
273+
#loop through the deps, check if any have the TARGET_TYPE of entrypoint_target_type, and print a warning if they do.
274+
if(LIBC_CMAKE_VERBOSE_LOGGING)
275+
set(entrypoint_deps "")
276+
if(NOT "${fq_deps_list}" STREQUAL "")
277+
check_entrypoint_deps(entrypoint_deps ${fq_deps_list})
278+
endif()
279+
if(NOT "${entrypoint_deps}" STREQUAL "")
280+
message(WARNING "Entrypoint ${fq_target_name} depends on public entrypoint(s) ${entrypoint_deps}.
281+
Depending on public entrypoints is not allowed in internal code.")
282+
endif()
283+
endif()
284+
233285
set(full_deps_list ${fq_deps_list} libc.src.__support.common)
234286

235287
if(SHOW_INTERMEDIATE_OBJECTS)
@@ -390,8 +442,6 @@ function(add_entrypoint_object target_name)
390442
)
391443
endfunction(add_entrypoint_object)
392444

393-
set(ENTRYPOINT_EXT_TARGET_TYPE "ENTRYPOINT_EXT")
394-
395445
# A rule for external entrypoint targets.
396446
# Usage:
397447
# add_entrypoint_external(

0 commit comments

Comments
 (0)