robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1# CMAKE REFERENCE 2# - intro: https://codingnest.com/basic-cmake/ 3# - best practices (3.0+): https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1 4# - pitfalls: https://izzys.casa/2019/02/everything-you-never-wanted-to-know-about-cmake/ 5# - troubleshooting: 6# - variable_watch https://cmake.org/cmake/help/latest/command/variable_watch.html 7# - verbose output: cmake --build build --verbose 8 9# Version should match the tested CMAKE_URL in .github/workflows/build.yml. 10cmake_minimum_required ( VERSION 3.16 ) 11 12project ( nvim C ) 13 14if ( POLICY CMP0135) 15cmake_policy ( SET CMP0135 NEW ) 16endif () 17 18if ( XCODE) 19message ( FATAL_ERROR [[Xcode generator is not supported. Use "Ninja" or "Unix Makefiles" instead]] ) 20endif () 21 22# Point CMake at any custom modules we may ship 23list ( APPEND CMAKE_MODULE_PATH " ${ PROJECT_SOURCE_DIR } /cmake" ) 24 25include ( CheckCCompilerFlag ) 26include ( CheckCSourceCompiles ) 27include ( CheckLibraryExists ) 28include ( ExternalProject ) 29include ( FindPackageHandleStandardArgs ) 30include ( GNUInstallDirs ) 31 32include ( Deps ) 33include ( Find ) 34include ( InstallHelpers ) 35include ( PreventInTreeBuilds ) 36include ( Util ) 37 38if ( NOT PROJECT_SOURCE_DIRSTREQUAL PROJECT_BINARY_DIR) 39# Auto-create a .gitignore in the specified "build" directory. 40file ( GENERATE OUTPUT .gitignore CONTENT "*" ) 41endif () 42 43#------------------------------------------------------------------------------- 44# User settings 45#------------------------------------------------------------------------------- 46 47if ( NOT DEPS_IGNORE_SHA) 48set ( DEPS_IGNORE_SHA FALSE ) 49endif () 50 51#------------------------------------------------------------------------------- 52# Variables 53#------------------------------------------------------------------------------- 54set ( FUNCS_DATA ${ PROJECT_BINARY_DIR } /funcs_data.mpack ) 55set ( TOUCHES_DIR ${ PROJECT_BINARY_DIR } /touches ) 56set ( VTERM_TEST_FILE ${ PROJECT_BINARY_DIR } /test/vterm_test_output ) 57 58file ( GLOB DOCFILES CONFIGURE_DEPENDS ${ PROJECT_SOURCE_DIR } /runtime/doc/*.txt ) 59 60if ( NOT CI_BUILD) 61set ( CMAKE_INSTALL_MESSAGE NEVER ) 62endif () 63 64if (${ CMAKE_VERSION } VERSION_LESS 3.20) 65set ( CMAKE_EXPORT_COMPILE_COMMANDS ON ) 66endif () 67 68if (${ CMAKE_VERSION } VERSION_GREATER_EQUAL 3.26) 69set ( COPY_DIRECTORY copy_directory_if_different ) 70else () 71set ( COPY_DIRECTORY copy_directory ) 72endif () 73 74# Prefer our bundled versions of dependencies. 75if ( DEFINED ENV{DEPS_BUILD_DIR}) 76set ( DEPS_PREFIX " $ ENV { DEPS_BUILD_DIR } /usr" CACHE PATH "Path prefix for finding dependencies" ) 77else () 78set ( DEPS_PREFIX " ${ CMAKE_CURRENT_SOURCE_DIR } /.deps/usr" CACHE PATH "Path prefix for finding dependencies" ) 79# When running from within CLion or Visual Studio, 80# build bundled dependencies automatically. 81if ( NOT EXISTS ${ DEPS_PREFIX } 82AND ( DEFINED ENV{CLION_IDE} 83OR DEFINED ENV{VisualStudioEdition})) 84message ( STATUS "Building dependencies..." ) 85set ( DEPS_BUILD_DIR ${ PROJECT_BINARY_DIR } /.deps ) 86file ( MAKE_DIRECTORY ${ DEPS_BUILD_DIR }) 87execute_process ( 88COMMAND ${ CMAKE_COMMAND } -G ${ CMAKE_GENERATOR } 89-D CMAKE_TOOLCHAIN_FILE= ${ CMAKE_TOOLCHAIN_FILE } 90-D CMAKE_BUILD_TYPE= ${ CMAKE_BUILD_TYPE } 91-D CMAKE_C_COMPILER= ${ CMAKE_C_COMPILER } 92-D CMAKE_C_FLAGS= ${ CMAKE_C_FLAGS } 93-D CMAKE_C_FLAGS_DEBUG= ${ CMAKE_C_FLAGS_DEBUG } 94-D CMAKE_C_FLAGS_MINSIZEREL= ${ CMAKE_C_FLAGS_MINSIZEREL } 95-D CMAKE_C_FLAGS_RELWITHDEBINFO= ${ CMAKE_C_FLAGS_RELWITHDEBINFO } 96-D CMAKE_C_FLAGS_RELEASE= ${ CMAKE_C_FLAGS_RELEASE } 97-D CMAKE_MAKE_PROGRAM= ${ CMAKE_MAKE_PROGRAM } 98${ PROJECT_SOURCE_DIR } /cmake.deps 99WORKING_DIRECTORY ${ DEPS_BUILD_DIR }) 100execute_process ( 101COMMAND ${ CMAKE_COMMAND } --build ${ DEPS_BUILD_DIR } 102--config ${ CMAKE_BUILD_TYPE }) 103set ( DEPS_PREFIX ${ DEPS_BUILD_DIR } /usr ) 104endif () 105endif () 106 107list ( INSERT CMAKE_PREFIX_PATH 0 ${ DEPS_PREFIX }) 108 109if ( APPLE) 110# If the macOS deployment target is not set manually (via $MACOSX_DEPLOYMENT_TARGET), 111# fall back to local system version. Needs to be done both here and in cmake.deps. 112if ( NOT CMAKE_OSX_DEPLOYMENT_TARGET) 113execute_process ( COMMAND sw_vers -productVersion 114OUTPUT_VARIABLE MACOS_VERSION 115OUTPUT_STRIP_TRAILING_WHITESPACE ) 116set ( CMAKE_OSX_DEPLOYMENT_TARGET " ${ MACOS_VERSION } " ) 117endif () 118message ( STATUS "Using deployment target ${ CMAKE_OSX_DEPLOYMENT_TARGET } " ) 119endif () 120 121if ( WIN32OR APPLE) 122# Handle case-insensitive filenames for Windows and Mac. 123set ( CASE_INSENSITIVE_FILENAME TRUE ) 124endif () 125 126if ( MINGW) 127# Disable LTO by default as it may not compile 128# See https://github.com/Alexpux/MINGW-packages/issues/3516 129# and https://github.com/neovim/neovim/pull/8654#issuecomment-402316672 130option ( ENABLE_LTO "enable link time optimization" OFF ) 131else () 132option ( ENABLE_LTO "enable link time optimization" ON ) 133endif () 134option ( ENABLE_LIBINTL "enable libintl" ON ) 135option ( ENABLE_UNIBILIUM "enable unibilium" ON ) 136option ( ENABLE_WASMTIME "enable wasmtime" OFF ) 137 138message ( STATUS "CMAKE_INSTALL_PREFIX= ${ CMAKE_INSTALL_PREFIX } " ) 139 140set_default_buildtype ( Debug ) 141get_property ( isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG ) 142if ( NOT isMultiConfig) 143# Unlike build dependencies in cmake.deps, we want dev dependencies such as 144# Uncrustify to always be built with Release. 145list ( APPEND DEPS_CMAKE_ARGS -D CMAKE_BUILD_TYPE=Release ) 146endif () 147 148# If not in a git repo (e.g., a tarball) these tokens define the complete 149# version string, else they are combined with the result of `git describe`. 150set ( NVIM_VERSION_MAJOR 0 ) 151set ( NVIM_VERSION_MINOR 13 ) 152set ( NVIM_VERSION_PATCH 0 ) 153set ( NVIM_VERSION_PRERELEASE "-dev" ) # for package maintainers 154 155# API level 156set ( NVIM_API_LEVEL 15 ) # Bump this after any API/stdlib change. 157set ( NVIM_API_LEVEL_COMPAT 0 ) # Adjust this after a _breaking_ API change. 158set ( NVIM_API_PRERELEASE true ) 159 160# We _want_ assertions in RelWithDebInfo build-type. 161if ( CMAKE_C_FLAGS_RELWITHDEBINFOMATCHES DNDEBUG) 162string ( REPLACE "-DNDEBUG" "-DRELDEBUG" CMAKE_C_FLAGS_RELWITHDEBINFO " ${ CMAKE_C_FLAGS_RELWITHDEBINFO } " ) 163string ( REPLACE "/DNDEBUG" "/DRELDEBUG" CMAKE_C_FLAGS_RELWITHDEBINFO " ${ CMAKE_C_FLAGS_RELWITHDEBINFO } " ) 164string ( REPLACE " " " " CMAKE_C_FLAGS_RELWITHDEBINFO " ${ CMAKE_C_FLAGS_RELWITHDEBINFO } " ) # Remove duplicate whitespace 165endif () 166 167option ( ENABLE_ASAN_UBSAN "Enable Clang address & undefined behavior sanitizer for nvim binary." OFF ) 168option ( ENABLE_MSAN "Enable Clang memory sanitizer for nvim binary." OFF ) 169# TSAN exists to test Luv threads. 170option ( ENABLE_TSAN "Enable Clang thread sanitizer for nvim binary." OFF ) 171 172if (( ENABLE_ASAN_UBSANAND ENABLE_MSAN) 173OR ( ENABLE_ASAN_UBSANAND ENABLE_TSAN) 174OR ( ENABLE_MSANAND ENABLE_TSAN)) 175message ( FATAL_ERROR "Sanitizers cannot be enabled simultaneously" ) 176endif () 177 178# Place targets in bin/ or lib/ for all build configurations 179set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /bin ) 180set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /lib ) 181set ( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ CMAKE_BINARY_DIR } /lib ) 182foreach ( CFGNAME${ CMAKE_CONFIGURATION_TYPES }) 183string ( TOUPPER ${ CFGNAME } CFGNAME ) 184set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY_ ${ CFGNAME } ${ CMAKE_BINARY_DIR } /bin ) 185set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY_ ${ CFGNAME } ${ CMAKE_BINARY_DIR } /lib ) 186set ( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_ ${ CFGNAME } ${ CMAKE_BINARY_DIR } /lib ) 187endforeach () 188 189if ( NOT PREFER_LUA) 190find_program ( LUA_PRG NAMES luajit ) 191endif () 192find_program ( LUA_PRG NAMES lua5.1 lua5.2 lua ) 193mark_as_advanced ( LUA_PRG ) 194if ( NOT LUA_PRG) 195message ( FATAL_ERROR "Failed to find a Lua 5.1-compatible interpreter" ) 196endif () 197message ( STATUS "Using Lua interpreter: ${ LUA_PRG } " ) 198 199# Some of the code generation still relies on stable table ordering in order to 200# produce reproducible output - specifically the msgpack'ed data in 201# funcs_metadata.generated.h and ui_events_metadata.generated.h. This should 202# ideally be fixed in the generators, but until then as a workaround you may provide 203# a specific lua implementation that provides the needed stability by setting LUA_GEN_PRG: 204if ( NOT LUA_GEN_PRG) 205set ( LUA_GEN_PRG " ${ LUA_PRG } " CACHE FILEPATH "Path to the lua used for code generation." ) 206endif () 207mark_as_advanced ( LUA_GEN_PRG ) 208message ( STATUS "Using Lua interpreter for code generation: ${ LUA_GEN_PRG } " ) 209 210option ( COMPILE_LUA "Pre-compile Lua sources into bytecode (for sources that are included in the binary)" ON ) 211 212# Lint 213option ( CI_LINT "Abort if lint programs not found" OFF ) 214if ( CI_LINT) 215set ( LINT_REQUIRED "REQUIRED" ) 216endif () 217find_program ( SHELLCHECK_PRG shellcheck ${ LINT_REQUIRED }) 218mark_as_advanced ( SHELLCHECK_PRG ) 219find_program ( STYLUA_PRG stylua ${ LINT_REQUIRED }) 220mark_as_advanced ( STYLUA_PRG ) 221find_program ( TS_QUERY_LS_PRG ts_query_ls ${ LINT_REQUIRED }) 222mark_as_advanced ( TS_QUERY_LS_PRG ) 223 224set ( STYLUA_DIRS runtime scripts src test contrib ) 225 226add_glob_target ( 227TARGET lintlua-stylua 228COMMAND ${ STYLUA_PRG } 229FLAGS --color=always --check --respect-ignores 230GLOB_DIRS ${ STYLUA_DIRS } 231GLOB_PAT *.lua 232TOUCH_STRATEGY PER_DIR ) 233# Special handling of some files (which are ignored in .styluaignore). 234# Workaround because stylua doesn't(?) support file-specific settings. 235add_custom_target ( lintlua-stylua2 236COMMAND ${ STYLUA_PRG } --config-path " ${ PROJECT_SOURCE_DIR } /.stylua2.toml" 237--color=always --check 238" ${ PROJECT_SOURCE_DIR } /test/functional/ui/decorations_spec.lua" 239" ${ PROJECT_SOURCE_DIR } /test/functional/ui/float_spec.lua" 240" ${ PROJECT_SOURCE_DIR } /test/functional/ui/multigrid_spec.lua" 241) 242add_dependencies ( lintlua-stylua lintlua-stylua2 ) 243 244add_custom_target ( lintlua ) 245add_dependencies ( lintlua lintlua-stylua ) 246 247add_glob_target ( 248TARGET lintsh 249COMMAND ${ SHELLCHECK_PRG } 250FLAGS -x -a 251GLOB_DIRS scripts 252GLOB_PAT *.sh 253TOUCH_STRATEGY PER_DIR ) 254 255add_custom_target ( lintquery-compilation 256COMMAND ${ TS_QUERY_LS_PRG } 257check ${ PROJECT_SOURCE_DIR } /runtime/queries --config 258'{ "parser_install_directories" : [ " ${ DEPS_PREFIX } /lib/nvim/parser" ]}' ) 259 260add_custom_target ( lintquery-format 261COMMAND ${ TS_QUERY_LS_PRG } 262format ${ PROJECT_SOURCE_DIR } /runtime/queries --check ) 263 264add_custom_target ( lintquery ) 265add_dependencies ( lintquery lintquery-compilation lintquery-format ) 266 267add_custom_target ( lintcommit 268COMMAND $ <TARGET_FILE:nvim_bin> --clean -l ${ PROJECT_SOURCE_DIR } /scripts/lintcommit.lua main ) 269add_dependencies ( lintcommit nvim_bin ) 270 271add_custom_target ( linterrcodes 272COMMAND $ <TARGET_FILE:nvim_bin> --clean -l ${ PROJECT_SOURCE_DIR } /scripts/linterrcodes.lua 273WORKING_DIRECTORY ${ PROJECT_SOURCE_DIR }) 274add_dependencies ( linterrcodes nvim_bin ) 275 276add_custom_target ( lint ) 277add_dependencies ( lint lintc lintlua lintsh lintquery ) 278 279# Format 280add_glob_target ( 281TARGET formatlua 282COMMAND ${ STYLUA_PRG } 283FLAGS --respect-ignores 284GLOB_DIRS ${ STYLUA_DIRS } 285GLOB_PAT *.lua 286TOUCH_STRATEGY PER_DIR ) 287# Special handling of some files (which are ignored in .styluaignore). 288# Workaround because stylua doesn't(?) support file-specific settings. 289add_custom_target ( formatlua2 290COMMAND ${ STYLUA_PRG } --config-path " ${ PROJECT_SOURCE_DIR } /.stylua2.toml" 291" ${ PROJECT_SOURCE_DIR } /test/functional/ui/decorations_spec.lua" 292" ${ PROJECT_SOURCE_DIR } /test/functional/ui/float_spec.lua" 293" ${ PROJECT_SOURCE_DIR } /test/functional/ui/multigrid_spec.lua" 294) 295add_dependencies ( formatlua formatlua2 ) 296 297add_custom_target ( formatquery 298COMMAND ${ TS_QUERY_LS_PRG } 299format ${ PROJECT_SOURCE_DIR } /runtime/queries ) 300 301add_custom_target ( format ) 302add_dependencies ( format formatc formatlua formatquery ) 303 304install_helper ( 305FILES ${ CMAKE_SOURCE_DIR } /src/man/nvim.1 306DESTINATION ${ CMAKE_INSTALL_MANDIR } /man1 ) 307 308add_custom_target ( nvim ALL ) 309add_dependencies ( nvim nvim_bin nvim_runtime_deps nvim_runtime ) 310 311add_subdirectory ( src/nvim ) 312add_subdirectory ( src/tee ) 313add_subdirectory ( src/xxd ) 314add_subdirectory ( cmake.config ) 315add_subdirectory ( runtime ) 316add_subdirectory ( test ) 317 318add_custom_target ( uninstall 319COMMAND ${ CMAKE_COMMAND } -P ${ PROJECT_SOURCE_DIR } /cmake/UninstallHelper.cmake ) 320 321if (${ CMAKE_SOURCE_DIR } STREQUAL ${ CMAKE_CURRENT_SOURCE_DIR }) 322add_subdirectory ( cmake.packaging ) 323endif () 324 325get_externalproject_options ( uncrustify ${ DEPS_IGNORE_SHA }) 326ExternalProject_Add ( uncrustify 327DOWNLOAD_DIR ${ DEPS_DOWNLOAD_DIR } /uncrustify 328CMAKE_ARGS ${ DEPS_CMAKE_ARGS } 329-D CMAKE_RUNTIME_OUTPUT_DIRECTORY= ${ DEPS_BIN_DIR } 330-D CMAKE_SKIP_RPATH=true 331EXCLUDE_FROM_ALL TRUE 332${ EXTERNALPROJECT_OPTIONS }) 333 334if ( CMAKE_HOST_SYSTEM_PROCESSORMATCHES "arm|aarch|ARM" ) 335set ( EMMYLUA_ARCH arm64 ) 336else () 337set ( EMMYLUA_ARCH x64 ) 338endif () 339 340string ( TOLOWER ${ CMAKE_HOST_SYSTEM_NAME } EMMYLUA_SYSTEM ) 341set ( EMMYLUA_ARCHIVE_EXT tar.gz ) 342set ( EMMYLUA_EXE_EXT "" ) 343if ( CMAKE_HOST_SYSTEM_NAMESTREQUAL "Linux" ) 344if ( EMMYLUA_ARCHSTREQUAL "arm64" ) 345set ( EMMYLUA_ARCH aarch64 ) 346endif () 347string ( APPEND EMMYLUA_ARCH "-glibc.2.17" ) 348elseif ( CMAKE_HOST_WIN32) 349set ( EMMYLUA_SYSTEM win32 ) 350set ( EMMYLUA_ARCHIVE_EXT zip ) 351set ( EMMYLUA_EXE_EXT .exe ) 352endif () 353 354set ( EMMYLUA_VERSION 0.25.1 ) 355set ( EMMYLUA_ARCHIVE "emmylua_check- ${ EMMYLUA_SYSTEM } - ${ EMMYLUA_ARCH } . ${ EMMYLUA_ARCHIVE_EXT } " ) 356set ( EMMYLUA_URL https://github.com/EmmyLuaLs/emmylua-analyzer-rust/releases/download/ ${ EMMYLUA_VERSION } / ${ EMMYLUA_ARCHIVE }) 357 358ExternalProject_Add ( download_emmylua 359URL ${ EMMYLUA_URL } 360DOWNLOAD_DIR ${ DEPS_DOWNLOAD_DIR } /emmylua 361SOURCE_DIR ${ DEPS_BIN_DIR } /emmylua 362CONFIGURE_COMMAND "" 363BUILD_COMMAND "" 364INSTALL_COMMAND "" 365EXCLUDE_FROM_ALL TRUE 366DOWNLOAD_NO_PROGRESS TRUE 367CMAKE_CACHE_ARGS ${ DEPS_CMAKE_CACHE_ARGS }) 368 369add_custom_target ( emmylua-check 370COMMAND ${ DEPS_BIN_DIR } /emmylua/emmylua_check ${ EMMYLUA_EXE_EXT } 371${ PROJECT_SOURCE_DIR } /runtime 372--config ${ PROJECT_SOURCE_DIR } /.emmyrc.json 373--warnings-as-errors 374WORKING_DIRECTORY ${ PROJECT_SOURCE_DIR } 375USES_TERMINAL 376VERBATIM ) 377 378add_dependencies ( emmylua-check download_emmylua )