From af440fd2969ff0c4e76927c8b9b4baafb3ff6651 Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Tue, 16 Jun 2026 23:59:51 +0200 Subject: [PATCH] Fixed clangd problems with emscripten. --- .clangd | 27 +++++++++++++++++++++++ CMakeLists.txt | 32 ++++++++++++++++++--------- README.md | 2 +- clean_compile_commands.py | 42 ++++++++++++++++++++++++++++++++++++ compile_commands.json | 1 + douwco_hivemind/src/Loop.cpp | 2 +- 6 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 .clangd create mode 100644 clean_compile_commands.py create mode 120000 compile_commands.json diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..abcc922 --- /dev/null +++ b/.clangd @@ -0,0 +1,27 @@ +CompileFlags: + # Add Emscripten include paths + Add: + - "-isystem" + - "/mnt/douwe/hdd/Projects/Screeps/emsdk/upstream/emscripten/system/include" + - "-isystem" + - "/mnt/douwe/hdd/Projects/Screeps/emsdk/upstream/emscripten/system/lib" + - "-isystem" + - "/mnt/douwe/hdd/Projects/Screeps/emsdk/upstream/emscripten/cache/sysroot/include" + - "-D__EMSCRIPTEN__" + - "-D__EMSCRIPTEN_PTHREADS__" + - "-I/mnt/douwe/hdd/Projects/Screeps/screepsxx/include" # Path to Screeps headers + # Remove ALL Emscripten-specific flags + Remove: + - "-s.*" # Removes ALL -s* flags (e.g., -sSTRICT=0, -sMODULARIZE=1) + - "--no-entry" + - "--bind" + - "--cache" + - "-sEXPORT_ES6=.*" + - "-sEXPORTED_RUNTIME_METHODS=.*" + - "-sASSERTIONS=.*" + - "-sMALLOC=.*" + - "-sEXPORTED_FUNCTIONS=.*" + +# Suppress diagnostics for unused includes and unknown arguments +Diagnostics: + UnusedIncludes: None \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 47d3b72..b0f72ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ project(douwco_hivemind CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) +# Generate compile commands +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + # Set the Emscripten toolchain set(CMAKE_TOOLCHAIN_FILE ${EMSCRIPTEN_ROOT}/cmake/Modules/Platform/Emscripten.cmake CACHE STRING "") @@ -21,24 +24,33 @@ set(TARGET_NAME douwco_hivemind) include_directories(${CMAKE_SOURCE_DIR}/douwco_hivemind/include) - file(GLOB SRC_FILES ${CMAKE_SOURCE_DIR}/douwco_hivemind/src/*.cpp ${CMAKE_SOURCE_DIR}/douwco_hivemind/src/*/*.cpp) add_executable(${TARGET_NAME} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} screepsxx) target_link_options(${TARGET_NAME} PUBLIC -sMODULARIZE=1 --no-entry --bind -sEXPORT_ES6=0) +# Post-process compile_commands.json to remove Emscripten flags +add_custom_command( + TARGET ${TARGET_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo "Cleaning compile_commands.json for clangd..." + COMMAND python3 ${CMAKE_SOURCE_DIR}/clean_compile_commands.py + ${CMAKE_BINARY_DIR}/compile_commands.json + COMMENT "Post-processing compile_commands.json for clangd compatibility" +) + +# Symlink compile_commands.json to project root for clangd +add_custom_command( + TARGET ${TARGET_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E create_symlink + ${CMAKE_BINARY_DIR}/compile_commands.json + ${CMAKE_SOURCE_DIR}/compile_commands.json + COMMENT "Symlinking compile_commands.json to project root for clangd" +) + # Collect all artifacts in 'dist' directory # WASM-module and corresponding JS-module must have different base names in order to use them in Screeps, so we add suffixes. add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${TARGET_NAME}.wasm ${CMAKE_SOURCE_DIR}/dist/${TARGET_NAME}_module.wasm) add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${TARGET_NAME}.js ${CMAKE_SOURCE_DIR}/dist/${TARGET_NAME}_loader.js) add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/main.js ${CMAKE_SOURCE_DIR}/dist/main.js) -add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/wasm_loader.js ${CMAKE_SOURCE_DIR}/dist/wasm_loader.js) - -# Following post-build step will automatically upload artifacts to -# official Screeps server. If you want to use it, please, -# set SCREEPS_TOKEN environment variable to your Screeps API token -# (https://docs.screeps.com/auth-tokens.html). -# If you don't want to use this script, please, remove following lines. -#find_package(Python COMPONENTS Interpreter REQUIRED) -#add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${Python_EXECUTABLE} ${screepsxx_SOURCE_DIR}/tools/upload.py ${CMAKE_SOURCE_DIR}/dist $ENV{SCREEPS_TOKEN}) +add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/wasm_loader.js ${CMAKE_SOURCE_DIR}/dist/wasm_loader.js) \ No newline at end of file diff --git a/README.md b/README.md index 7db6ca4..6aae4e3 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Create the makefiles using cmake. For more info look at the readme in screepsxx. ``` mkdir build cd build -cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake .. +cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake .. ``` # Build diff --git a/clean_compile_commands.py b/clean_compile_commands.py new file mode 100644 index 0000000..9d7a5f1 --- /dev/null +++ b/clean_compile_commands.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import json +import re +import sys +import os + +def clean_compile_commands(compile_commands_path): + # Load the compile_commands.json file + with open(compile_commands_path, 'r') as f: + data = json.load(f) + + # Flags to remove (Emscripten-specific) + flags_to_remove = [ + r'-s\w+=\S*', # Matches -sSTRICT=0, -sMODULARIZE=1, etc. + r'-s\w+', # Matches -sWASM, -sASSERTIONS, etc. + r'--no-entry', + r'--bind', + r'--cache', + ] + + # Clean each command + for entry in data: + if 'command' in entry: + command = entry['command'] + # Remove all unwanted flags + for flag_pattern in flags_to_remove: + command = re.sub(flag_pattern, '', command) + # Remove extra spaces + command = ' '.join(command.split()) + entry['command'] = command + + # Save the cleaned file + with open(compile_commands_path, 'w') as f: + json.dump(data, f, indent=2) + + print(f"Cleaned {compile_commands_path}") + +if __name__ == '__main__': + if len(sys.argv) != 2: + print("Usage: python clean_compile_commands.py ") + sys.exit(1) + clean_compile_commands(sys.argv[1]) \ No newline at end of file diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..4ef3144 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +/mnt/douwe/hdd/Projects/Screeps/build/compile_commands.json \ No newline at end of file diff --git a/douwco_hivemind/src/Loop.cpp b/douwco_hivemind/src/Loop.cpp index 46a697f..a6252dd 100644 --- a/douwco_hivemind/src/Loop.cpp +++ b/douwco_hivemind/src/Loop.cpp @@ -9,7 +9,7 @@ #include "Engine.hpp" #include "Structures/Spawn.hpp" -//EMSCRIPTEN_KEEPALIVE +EMSCRIPTEN_KEEPALIVE extern "C" void loop() { Screeps::Context::update();