Skip to content

[libc] Fix the api-test integration test #79627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions libc/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ if(LIBC_TARGET_ARCHITECTURE_IS_GPU AND
return()
endif()

add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(utils)

if(LLVM_LIBC_FULL_BUILD AND NOT LIBC_TARGET_OS_IS_BAREMETAL)
add_subdirectory(IntegrationTest)
endif()

add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(utils) # uses IntegrationTest

if(NOT LLVM_LIBC_FULL_BUILD)
return()
endif()
Expand Down
33 changes: 20 additions & 13 deletions libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,38 @@ if(LLVM_RUNTIMES_BUILD OR LIBC_HDRGEN_EXE)
return()
endif()

set(public_test ${CMAKE_CURRENT_BINARY_DIR}/public_api_test.cpp)

# Generate api public entrypoints.
set(entrypoints_name_list "")
foreach(entry IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
get_target_property(entry_name ${entry} "ENTRYPOINT_NAME")
list(APPEND entrypoints_name_list ${entry_name})
endforeach()

# TODO: Remove these when they are added to the TableGen.
list(REMOVE_ITEM entrypoints_name_list "__assert_fail" "__errno_location")
list(TRANSFORM entrypoints_name_list PREPEND "-e=")
list(REMOVE_ITEM entrypoints_name_list "prctl" "getauxval" "__stack_chk_fail")
string(REPLACE ";" "," entrypoints_name_list "${entrypoints_name_list}")

file(GLOB spec_files ${LIBC_SOURCE_DIR}/spec/*.td)
set(public_entrypoints ${CMAKE_CURRENT_BINARY_DIR}/public_entrypoints.txt)
file(WRITE ${public_entrypoints} "${entrypoints_name_list}")

# Generate api test souce code.
set(public_test ${CMAKE_CURRENT_BINARY_DIR}/public_api_test.cpp)
file(GLOB spec_files ${LIBC_SOURCE_DIR}/spec/*.td)
add_custom_command(
OUTPUT ${public_test}
COMMAND $<TARGET_FILE:libc-prototype-testgen> -o ${public_test}
${entrypoints_name_list}
-I ${LIBC_SOURCE_DIR}
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td

DEPENDS ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td ${spec_files}
libc-prototype-testgen ${TARGET_PUBLIC_HEADERS}
${LIBC_TARGET}
COMMAND
"$<TARGET_FILE:libc-prototype-testgen>"
-o ${public_test}
--entrypoints_file ${public_entrypoints}
-I ${LIBC_SOURCE_DIR}
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
DEPENDS
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
${spec_files}
${public_entrypoints}
libc-prototype-testgen
${TARGET_PUBLIC_HEADERS}
${LIBC_TARGET}
)

add_custom_target(libc-api-test)
Expand Down
16 changes: 14 additions & 2 deletions libc/utils/HdrGen/PrototypeTestGen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
include(TableGen)

set(LLVM_LINK_COMPONENTS Support)

add_tablegen(libc-prototype-testgen LLVM_LIBC
PrototypeTestGen.cpp
)
target_link_libraries(libc-prototype-testgen PRIVATE LibcTableGenUtil)
target_include_directories(libc-prototype-testgen PRIVATE ${LIBC_SOURCE_DIR})
target_link_libraries(libc-prototype-testgen
PRIVATE
LibcTableGenUtil
)
target_include_directories(libc-prototype-testgen
PRIVATE
${LIBC_SOURCE_DIR}
${LLVM_INCLUDE_DIR}
${LLVM_MAIN_INCLUDE_DIR}
)
29 changes: 23 additions & 6 deletions libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@

#include "utils/LibcTableGenUtil/APIIndexer.h"

#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/TableGen/Main.h"
#include "llvm/TableGen/Record.h"

namespace {

llvm::cl::list<std::string>
EntrypointNamesOption("e", llvm::cl::desc("<list of entrypoints>"),
llvm::cl::OneOrMore);
llvm::cl::opt<std::string> EntrypointsFilename(
"entrypoints_file",
llvm::cl::desc("file containing the comma separated list of entrypoints"),
llvm::cl::Required);

} // anonymous namespace

bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) {
OS << "#include \"src/__support/CPP/type_traits.h\"\n";
llvm_libc::APIIndexer G(records);
std::unordered_set<std::string> headerFileSet;
for (const auto &entrypoint : EntrypointNamesOption) {

auto Entrypoints = llvm::MemoryBuffer::getFile(EntrypointsFilename);

std::vector<std::string> entrypoints;
for (auto entrypoint : llvm::split((*Entrypoints)->getBuffer(), ','))
entrypoints.push_back(entrypoint.str());
for (auto entrypoint : entrypoints) {
if (entrypoint == "errno")
continue;
auto match = G.FunctionToHeaderMap.find(entrypoint);
Expand All @@ -48,7 +57,7 @@ bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) {
OS << '\n';

OS << "extern \"C\" int main() {\n";
for (const auto &entrypoint : EntrypointNamesOption) {
for (const auto &entrypoint : entrypoints) {
if (entrypoint == "errno")
continue;
auto match = G.FunctionSpecMap.find(entrypoint);
Expand Down Expand Up @@ -91,9 +100,17 @@ bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) {
<< " prototype in TableGen does not match public header" << '"';
OS << ");\n";
}
OS << '\n';
OS << " // Check that all entrypoints are present in the binary";
OS << " uintptr_t check = 0;\n";
for (const auto &entrypoint : entrypoints) {
if (entrypoint == "errno")
continue;
OS << " check += reinterpret_cast<uintptr_t>(&" << entrypoint << ");\n";
}

OS << '\n';
OS << " return 0;\n";
OS << " return check != 0 ? 0 : 1;\n";
OS << "}\n\n";

return false;
Expand Down