From 3711a0387a96d6531f3ae07a9b18fb8e2afc2e41 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sat, 27 Apr 2024 09:21:46 +0200 Subject: [PATCH] refactor(build): make all generated c files headers There's no "rule" or bad practice or whatever that says we cannot generate c files. it is is just that we have ~20 generated headers and ~2 generated sources and there is nothing in these two generated source files which sets them aparts. Lua bindings are not different from rpc bindings, and pathdef is not different from versiondef. So to simplify build logic and ease the future port to build.zig, streamline the build to only have generated headers, no direct generated .c files. Also "nlua_add_api_functions" had its prototype duplicated twice which defeated the point of having mandatory prototypes (one source of truth). --- cmake.config/CMakeLists.txt | 4 ++-- cmake.config/{pathdef.c.in => pathdef.h.in} | 1 - src/nvim/CMakeLists.txt | 14 ++------------ src/nvim/generators/gen_api_dispatch.lua | 19 ------------------- src/nvim/lua/api_wrappers.c | 18 ++++++++++++++++++ src/nvim/lua/executor.h | 2 +- src/nvim/os/env.c | 1 + 7 files changed, 24 insertions(+), 35 deletions(-) rename cmake.config/{pathdef.c.in => pathdef.h.in} (75%) create mode 100644 src/nvim/lua/api_wrappers.c diff --git a/cmake.config/CMakeLists.txt b/cmake.config/CMakeLists.txt index d30c522aa2..3171f9e88c 100644 --- a/cmake.config/CMakeLists.txt +++ b/cmake.config/CMakeLists.txt @@ -194,6 +194,6 @@ file(GENERATE INPUT "${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef.h.gen") configure_file ( - "${PROJECT_SOURCE_DIR}/cmake.config/pathdef.c.in" - "${PROJECT_BINARY_DIR}/cmake.config/auto/pathdef.c" + "${PROJECT_SOURCE_DIR}/cmake.config/pathdef.h.in" + "${PROJECT_BINARY_DIR}/cmake.config/auto/pathdef.h" ESCAPE_QUOTES) diff --git a/cmake.config/pathdef.c.in b/cmake.config/pathdef.h.in similarity index 75% rename from cmake.config/pathdef.c.in rename to cmake.config/pathdef.h.in index 773fa1d592..bc09549fb7 100644 --- a/cmake.config/pathdef.c.in +++ b/cmake.config/pathdef.h.in @@ -1,4 +1,3 @@ -#include "${PROJECT_SOURCE_DIR}/src/nvim/vim_defs.h" char *default_vim_dir = "${CMAKE_INSTALL_FULL_DATAROOTDIR}/nvim"; char *default_vimruntime_dir = ""; char *default_lib_dir = "${CMAKE_INSTALL_FULL_LIBDIR}/nvim"; diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 64bd40f5f0..d9cc695c55 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -327,7 +327,7 @@ set(GENERATED_UI_EVENTS_CALL ${GENERATED_DIR}/ui_events_call.generated.h) set(GENERATED_UI_EVENTS_CLIENT ${GENERATED_DIR}/ui_events_client.generated.h) set(GENERATED_UI_EVENTS_REMOTE ${GENERATED_DIR}/ui_events_remote.generated.h) set(GENERATED_UNICODE_TABLES ${GENERATED_DIR}/unicode_tables.generated.h) -set(LUA_API_C_BINDINGS ${GENERATED_DIR}/lua_api_c_bindings.generated.c) +set(LUA_API_C_BINDINGS ${GENERATED_DIR}/lua_api_c_bindings.generated.h) set(VIM_MODULE_FILE ${GENERATED_DIR}/lua/vim_module.generated.h) # NVIM_RUNTIME_DIR @@ -505,7 +505,6 @@ set(LUA_GEN_DEPS ${GENERATOR_PRELOAD} $) # NVIM_GENERATED_FOR_HEADERS: generated headers to be included in headers # NVIM_GENERATED_FOR_SOURCES: generated headers to be included in sources -# NVIM_GENERATED_SOURCES: generated source files # These lists must be mutually exclusive. foreach(sfile ${NVIM_SOURCES} ${GENERATED_API_DISPATCH} @@ -620,10 +619,6 @@ add_custom_command( VERBATIM ) -list(APPEND NVIM_GENERATED_SOURCES - "${LUA_API_C_BINDINGS}" -) - add_custom_command( OUTPUT ${GENERATED_UI_EVENTS_CALL} ${GENERATED_UI_EVENTS_REMOTE} @@ -657,10 +652,7 @@ list(APPEND NVIM_GENERATED_FOR_SOURCES "${GENERATED_OPTIONS_MAP}" "${GENERATED_UNICODE_TABLES}" "${VIM_MODULE_FILE}" -) - -list(APPEND NVIM_GENERATED_SOURCES - "${PROJECT_BINARY_DIR}/cmake.config/auto/pathdef.c" + "${PROJECT_BINARY_DIR}/cmake.config/auto/pathdef.h" ) add_custom_command(OUTPUT ${GENERATED_EX_CMDS_ENUM} ${GENERATED_EX_CMDS_DEFS} @@ -709,7 +701,6 @@ endif() target_sources(main_lib INTERFACE ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} - ${NVIM_GENERATED_SOURCES} ${NVIM_SOURCES} ${NVIM_HEADERS} ${EXTERNAL_SOURCES} @@ -909,7 +900,6 @@ add_subdirectory(po) add_custom_target(generated-sources DEPENDS ${NVIM_GENERATED_FOR_HEADERS} ${NVIM_GENERATED_FOR_SOURCES} - ${NVIM_GENERATED_SOURCES} ) file(GLOB API_SOURCES CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/src/nvim/api/*.c) diff --git a/src/nvim/generators/gen_api_dispatch.lua b/src/nvim/generators/gen_api_dispatch.lua index b0f169ea6f..8ad5858c12 100644 --- a/src/nvim/generators/gen_api_dispatch.lua +++ b/src/nvim/generators/gen_api_dispatch.lua @@ -716,23 +716,6 @@ end -- start building lua output output = assert(io.open(lua_c_bindings_outputf, 'wb')) -output:write([[ -#include -#include -#include - -#include "nvim/ex_docmd.h" -#include "nvim/ex_getln.h" -#include "nvim/func_attr.h" -#include "nvim/globals.h" -#include "nvim/api/private/defs.h" -#include "nvim/api/private/helpers.h" -#include "nvim/api/private/dispatch.h" -#include "nvim/lua/converter.h" -#include "nvim/lua/executor.h" -#include "nvim/memory.h" - -]]) include_headers(output, headers) output:write('\n') @@ -974,8 +957,6 @@ end output:write(string.format( [[ -void nlua_add_api_functions(lua_State *lstate) - REAL_FATTR_NONNULL_ALL; void nlua_add_api_functions(lua_State *lstate) { lua_createtable(lstate, 0, %u); diff --git a/src/nvim/lua/api_wrappers.c b/src/nvim/lua/api_wrappers.c new file mode 100644 index 0000000000..2b7b0c6471 --- /dev/null +++ b/src/nvim/lua/api_wrappers.c @@ -0,0 +1,18 @@ +#include +#include +#include + +#include "nvim/api/private/defs.h" +#include "nvim/api/private/dispatch.h" +#include "nvim/api/private/helpers.h" +#include "nvim/ex_docmd.h" +#include "nvim/ex_getln.h" +#include "nvim/func_attr.h" +#include "nvim/globals.h" +#include "nvim/lua/converter.h" +#include "nvim/lua/executor.h" +#include "nvim/memory.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua_api_c_bindings.generated.h" +#endif diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index ebcd62122f..299df2fdde 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -12,7 +12,7 @@ #include "nvim/types_defs.h" #include "nvim/usercmd.h" // IWYU pragma: keep -// Generated by msgpack-gen.lua +// Generated by generators/gen_api_dispatch.lua void nlua_add_api_functions(lua_State *lstate) REAL_FATTR_NONNULL_ALL; typedef struct { diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index ed55f4f0eb..fbc3a243a6 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -49,6 +49,7 @@ #endif #ifdef INCLUDE_GENERATED_DECLARATIONS +# include "auto/pathdef.h" # include "os/env.c.generated.h" #endif