Skip to content
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

Meta: Pass compiler's builtin includes to jakt too #25573

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions Meta/CMake/jakt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,59 @@ if (NOT ENABLE_JAKT)
return()
endif()

# Get the C++ compiler's include dirs
# Equivalent to: $CXX -xc++ /dev/null -E -Wp,-v 2>&1 | sed -n 's,^ ,,p'
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} -xc++ /dev/null -E
OUTPUT_VARIABLE CXX_INCLUDES
ERROR_VARIABLE CXX_INCLUDES
)
string(REGEX MATCHALL "(^|\n) [^\n]*" CXX_INCLUDES "${CXX_INCLUDES}")
string(REGEX REPLACE "\n" "" CXX_INCLUDES "${CXX_INCLUDES}")
string(REGEX REPLACE "; +" ";" CXX_INCLUDES "${CXX_INCLUDES}")

# Get the builtin/system includes
# Equivalent to $CXX -E test.cpp |& sed -e '/^# 1 /p;d' | sed -e 's/# 1 "//;s/"[^"]*$//'
file(WRITE ${CMAKE_BINARY_DIR}/jakt_test.cpp "#include <new>")
Copy link
Member

@ADKaster ADKaster Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, this works. But would it be easier to understand if it was a python script instead?

I'm scared of regexes :)

Also why are there two commands? Surely This is good enough?

$ ./Toolchain/Local/aarch64/bin/aarch64-pc-serenity-gcc  -xc++ -E -Wp,-v - < /dev/null
ignoring nonexistent directory "/Users/andrew/Source/serenity/Toolchain/../Build/aarch64/Root/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
 /Users/andrew/Source/serenity/Toolchain/Local/aarch64/lib/gcc/aarch64-pc-serenity/13.2.0/../../../../aarch64-pc-serenity/include/c++/13.2.0
 /Users/andrew/Source/serenity/Toolchain/Local/aarch64/lib/gcc/aarch64-pc-serenity/13.2.0/../../../../aarch64-pc-serenity/include/c++/13.2.0/aarch64-pc-serenity
 /Users/andrew/Source/serenity/Toolchain/Local/aarch64/lib/gcc/aarch64-pc-serenity/13.2.0/../../../../aarch64-pc-serenity/include/c++/13.2.0/backward
 /Users/andrew/Source/serenity/Toolchain/Local/aarch64/lib/gcc/aarch64-pc-serenity/13.2.0/include
 /Users/andrew/Source/serenity/Toolchain/Local/aarch64/lib/gcc/aarch64-pc-serenity/13.2.0/include-fixed
 /Users/andrew/Source/serenity/Toolchain/Local/aarch64/lib/gcc/aarch64-pc-serenity/13.2.0/../../../../aarch64-pc-serenity/include
 /Users/andrew/Source/serenity/Toolchain/../Build/aarch64/Root/usr/include
End of search list.
# 0 "<stdin>"
# 0 "<built-in>"
# 0 "<command-line>"
# 1 "<stdin>"
got_dotdotdot = False
paths = []
for path.strip() in lines:
    if not got_dotdotdot:
        if path == "#include "..." search starts here:"
            got_dotdotdot = True
         continue
    if path ==  "#include <...> search starts here:"
        continue
    if path == "End of search list."
        break    
    paths.append(pathlib.Path(path.split(" ")[0]).resolve())

?

execute_process(
COMMAND ${CMAKE_CXX_COMPILER} -E ${CMAKE_BINARY_DIR}/jakt_test.cpp
OUTPUT_VARIABLE CXX_SYSTEM_INCLUDES
ERROR_VARIABLE CXX_SYSTEM_INCLUDES
)
string(REGEX MATCHALL "\n# 1 [^\n]*\n" CXX_SYSTEM_INCLUDES "${CXX_SYSTEM_INCLUDES}")
string(REGEX REPLACE "# 1 \"" "" CXX_SYSTEM_INCLUDES "${CXX_SYSTEM_INCLUDES}")
string(REGEX REPLACE "\"[^\"]*\n" "\n" CXX_SYSTEM_INCLUDES "${CXX_SYSTEM_INCLUDES}")
string(REGEX REPLACE "\n" ";" CXX_SYSTEM_INCLUDES "${CXX_SYSTEM_INCLUDES}")

set(CXX_SYSTEM_INCLUDES_CLEAN)
foreach(include IN LISTS CXX_SYSTEM_INCLUDES)
# If the include is not a path, ignore it
if (NOT include MATCHES "^/")
continue()
endif()
# Get the dirname of the include
get_filename_component(include_dir ${include} DIRECTORY)
# If the dir is a subdir of any of CXX_INCLUDES, ignore it
set(skip FALSE)
foreach(cxx_include IN LISTS CXX_INCLUDES)
if (include_dir MATCHES "^${cxx_include}")
set(skip TRUE)
break()
endif()
endforeach()
if (skip)
continue()
endif()
# If the dir ends with /bits, strip the /bits segment
if (include_dir MATCHES "/bits$")
get_filename_component(include_dir ${include_dir} DIRECTORY)
endif()
list(APPEND CXX_SYSTEM_INCLUDES_CLEAN ${include_dir})
endforeach()

# Remove duplicates
list(REMOVE_DUPLICATES CXX_SYSTEM_INCLUDES_CLEAN)

cmake_host_system_information(RESULT JAKT_PROCESSOR_COUNT QUERY NUMBER_OF_PHYSICAL_CORES)
set_property(GLOBAL PROPERTY JOB_POOLS jakt_pool=1)

Expand All @@ -98,6 +151,12 @@ function(add_jakt_executable target source)
foreach(config IN LISTS JAKT_EXECUTABLE_CONFIGS)
list(APPEND configs "--config" "${config}")
endforeach()
foreach(include IN LISTS CXX_COMPILER_INCLUDES)
list(APPEND includes "-I" "${include}")
endforeach()
foreach(include IN LISTS CXX_SYSTEM_INCLUDES_CLEAN)
list(APPEND includes "--extra-cpp-flag-isystem${include}")
endforeach()
foreach(include IN LISTS JAKT_EXECUTABLE_INCLUDES)
list(APPEND includes "-I" "${include}")
endforeach()
Expand Down Expand Up @@ -198,6 +257,7 @@ function(serenity_jakt_app target_name source)
${PROJECT_BINARY_DIR}/Userland/Services
${PROJECT_BINARY_DIR}/Userland/Libraries
${PROJECT_BINARY_DIR}/Userland
${CMAKE_SYSROOT}/usr/include
${SERENITY_JAKT_EXECUTABLE_INCLUDES}
CONFIGS ${SERENITY_JAKT_EXECUTABLE_CONFIGS}
LINK_LIBRARIES ${SERENITY_JAKT_EXECUTABLE_LINK_LIBRARIES}
Expand All @@ -223,6 +283,7 @@ function(serenity_jakt_executable target_name)
${PROJECT_BINARY_DIR}/Userland/Services
${PROJECT_BINARY_DIR}/Userland/Libraries
${PROJECT_BINARY_DIR}/Userland
${CMAKE_SYSROOT}/usr/include
${SERENITY_JAKT_EXECUTABLE_INCLUDES}
CONFIGS ${SERENITY_JAKT_EXECUTABLE_CONFIGS}
LINK_LIBRARIES ${SERENITY_JAKT_EXECUTABLE_LINK_LIBRARIES}
Expand Down
4 changes: 2 additions & 2 deletions Toolchain/BuildJakt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ buildstep_ninja() {

mkdir -p "$DIR/Tarballs"

JAKT_COMMIT_HASH="1fed928d0abf08188e48fe765ab68e5047c05ec2"
JAKT_COMMIT_HASH="f1e80aa5cd96963919a127da200986791233b5e4"
JAKT_NAME="jakt-${JAKT_COMMIT_HASH}"
JAKT_TARBALL="${JAKT_NAME}.tar.gz"
JAKT_GIT_URL="https://github.com/serenityos/jakt"
JAKT_GIT_URL="https://github.com/alimpfard/jakt"

function already_available() {
local TOOLCHAIN="$1"; shift
Expand Down
Loading