robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
07b14c8e2e
master
1# Defines a target that depends on FILES and the files found by globbing 2# when using GLOB_PAT and GLOB_DIRS. The target will rerun if any files it 3# depends on has changed. Which files the target will run the command on 4# depends on the value of TOUCH_STRATEGY. 5# 6# Options: 7# 8# Single value arguments: 9# TARGET - Name of the target 10# COMMAND - Path of the command to be run 11# GLOB_PAT - Glob pattern to use. Only used if GLOB_DIRS is specified 12# TOUCH_STRATEGY - Specify touch strategy, meaning decide how to group files 13# and connect them to a specific touch file. 14# 15# For example, let us say we have file A and B and that we create a touch file 16# for each of them, TA and TB. This would essentially make file A and B 17# independent of each other, meaning that if I change file A and run the 18# target, then the target will only run its commands for file A and ignore 19# file B. 20# 21# Another example: let's say we have file A and B, but now we create only a 22# single touch file T for both of them. This would mean that if I change 23# either file A or B, then the target will run its commands on both A and B. 24# Meaning that even if I only change file A, the target will still run 25# commands on both A and B. 26# 27# The more touch files we create for a target, the fewer commands we'll need 28# to rerun, and by extension, the more time we'll save. Unfortunately, the 29# more touch files we create the more intermediary targets will be created, 30# one for each touch file. This makes listing all targets with 31# `cmake --build build --target help` less useful since each touch file will 32# be listed. The tradeoff that needs to be done here is between performance 33# and "discoverability". As a general guideline: the more popular a target is 34# and the more time it takes to run it, the more granular you want your touch 35# files to be. Conversely, if a target rarely needs to be run or if it's fast, 36# then you should create fewer targets. 37# 38# Possible values for TOUCH_STRATEGY: 39# "SINGLE": create a single touch file for all files. 40# "PER_FILE": create a touch file for each file. Defaults to this if 41# TOUCH_STRATEGY isn't specified. 42# "PER_DIR": create a touch file for each directory. 43# 44# List arguments: 45# FLAGS - List of flags to use after COMMAND 46# FILES - List of files to use COMMAND on. It's possible to combine this 47# with GLOB_PAT and GLOB_DIRS; the files found by globbing will 48# simple be added to FILES 49# GLOB_DIRS - The directories to recursively search for files with extension 50# GLOB_PAT 51# EXCLUDE - List of paths to skip (regex). Works on both directories and 52# files. 53function ( add_glob_target ) 54cmake_parse_arguments ( ARG 55"" 56"TARGET;COMMAND;GLOB_PAT;TOUCH_STRATEGY" 57"FLAGS;FILES;GLOB_DIRS;EXCLUDE" 58${ ARGN } 59) 60 61if ( NOT ARG_COMMAND) 62add_custom_target ( $ { ARG_TARGET }) 63add_custom_command ( TARGET ${ ARG_TARGET } 64POST_BUILD 65COMMAND ${ CMAKE_COMMAND } -E echo " ${ ARG_TARGET } SKIP: ${ ARG_COMMAND } not found" ) 66return () 67endif () 68 69foreach ( gd${ ARG_GLOB_DIRS }) 70file ( GLOB_RECURSE globfiles_unnormalized ${ PROJECT_SOURCE_DIR } / ${ gd } / ${ ARG_GLOB_PAT }) 71set ( globfiles ) 72foreach ( f${ globfiles_unnormalized }) 73file ( TO_CMAKE_PATH " ${ f } " f ) 74list ( APPEND globfiles ${ f }) 75endforeach () 76list ( APPEND ARG_FILES ${ globfiles }) 77endforeach () 78 79list ( APPEND ARG_EXCLUDE runtime/lua/vim/_meta ) # only generated files, always ignore 80foreach ( exclude_pattern${ ARG_EXCLUDE }) 81list ( FILTER ARG_FILES EXCLUDE REGEX ${ exclude_pattern }) 82endforeach () 83 84if ( NOT ARG_TOUCH_STRATEGY) 85set ( ARG_TOUCH_STRATEGY PER_FILE ) 86endif () 87set ( POSSIBLE_TOUCH_STRATEGIES SINGLE PER_FILE PER_DIR ) 88if ( NOT ARG_TOUCH_STRATEGYIN_LIST POSSIBLE_TOUCH_STRATEGIES) 89message ( FATAL_ERROR "Unrecognized value for TOUCH_STRATEGY: ${ ARG_TOUCH_STRATEGY } " ) 90endif () 91 92if ( ARG_TOUCH_STRATEGYSTREQUAL SINGLE) 93set ( touch_file ${ TOUCHES_DIR } / ${ ARG_TARGET }) 94add_custom_command ( 95OUTPUT ${ touch_file } 96COMMAND ${ CMAKE_COMMAND } -E touch ${ touch_file } 97COMMAND ${ ARG_COMMAND } ${ ARG_FLAGS } ${ ARG_FILES } 98WORKING_DIRECTORY ${ PROJECT_SOURCE_DIR } 99DEPENDS ${ ARG_FILES }) 100list ( APPEND touch_list ${ touch_file }) 101elseif ( ARG_TOUCH_STRATEGYSTREQUAL PER_FILE) 102set ( touch_dir ${ TOUCHES_DIR } / ${ ARG_TARGET }) 103file ( MAKE_DIRECTORY ${ touch_dir }) 104foreach ( f${ ARG_FILES }) 105string ( REGEX REPLACE "^ ${ PROJECT_SOURCE_DIR } /" "" tf ${ f }) 106string ( REGEX REPLACE "[/.]" "-" tf ${ tf }) 107set ( touch_file ${ touch_dir } / ${ tf }) 108add_custom_command ( 109OUTPUT ${ touch_file } 110COMMAND ${ CMAKE_COMMAND } -E touch ${ touch_file } 111COMMAND ${ ARG_COMMAND } ${ ARG_FLAGS } ${ f } 112WORKING_DIRECTORY ${ PROJECT_SOURCE_DIR } 113DEPENDS ${ f }) 114list ( APPEND touch_list ${ touch_file }) 115endforeach () 116elseif ( ARG_TOUCH_STRATEGYSTREQUAL PER_DIR) 117set ( touch_dirs ) 118foreach ( f${ ARG_FILES }) 119get_filename_component ( out ${ f } DIRECTORY ) 120list ( APPEND touch_dirs ${ out }) 121endforeach () 122list ( REMOVE_DUPLICATES touch_dirs ) 123 124foreach ( touch_dir${ touch_dirs }) 125set ( relevant_files ) 126foreach ( f${ ARG_FILES }) 127get_filename_component ( out ${ f } DIRECTORY ) 128if (${ touch_dir } STREQUAL ${ out }) 129list ( APPEND relevant_files ${ f }) 130endif () 131endforeach () 132 133set ( td ${ TOUCHES_DIR } / ${ ARG_TARGET }) 134file ( MAKE_DIRECTORY ${ td }) 135string ( REGEX REPLACE "^ ${ PROJECT_SOURCE_DIR } /" "" tf ${ touch_dir }) 136string ( REGEX REPLACE "[/.]" "-" tf ${ tf }) 137set ( touch_file ${ td } / ${ tf }) 138 139add_custom_command ( 140OUTPUT ${ touch_file } 141COMMAND ${ CMAKE_COMMAND } -E touch ${ touch_file } 142COMMAND ${ ARG_COMMAND } ${ ARG_FLAGS } ${ relevant_files } 143WORKING_DIRECTORY ${ PROJECT_SOURCE_DIR } 144DEPENDS ${ relevant_files }) 145list ( APPEND touch_list ${ touch_file }) 146endforeach () 147endif () 148 149add_custom_target ( $ { ARG_TARGET } DEPENDS ${ touch_list }) 150endfunction () 151 152# A wrapper function that combines add_custom_command and add_custom_target. It 153# essentially models the "make" dependency where a target is only rebuilt if 154# any dependencies have been changed. 155# 156# Important to note is that `DEPENDS` is a bit misleading; it should not only 157# specify dependencies but also the files that are being generated/output 158# files in order to work correctly. 159function ( add_target ) 160cmake_parse_arguments ( ARG 161"" 162"" 163"COMMAND;DEPENDS;CUSTOM_COMMAND_ARGS" 164${ ARGN } 165) 166set ( target ${ ARGV0 }) 167 168set ( touch_file ${ TOUCHES_DIR } / ${ target }) 169add_custom_command ( 170OUTPUT ${ touch_file } 171COMMAND ${ CMAKE_COMMAND } -E touch ${ touch_file } 172COMMAND ${ CMAKE_COMMAND } -E env "VIMRUNTIME= ${ NVIM_RUNTIME_DIR } " ${ ARG_COMMAND } 173WORKING_DIRECTORY ${ PROJECT_SOURCE_DIR } 174DEPENDS ${ ARG_DEPENDS } 175${ ARG_CUSTOM_COMMAND_ARGS }) 176add_custom_target ( $ { target } DEPENDS ${ touch_file }) 177endfunction () 178 179# Set default build type to BUILD_TYPE. 180# 181# The correct way to specify build type (for example Release) for 182# single-configuration generators (Make and Ninja) is to run 183# 184# cmake -B build -D CMAKE_BUILD_TYPE=Release 185# cmake --build build 186# 187# while for multi-configuration generators (Visual Studio, Xcode and Ninja 188# Multi-Config) is to run 189# 190# cmake -B build 191# cmake --build build --config Release 192# 193# Passing CMAKE_BUILD_TYPE for multi-config generators will not only not be 194# used, but also generate a warning for the user. 195function ( set_default_buildtype BUILD_TYPE ) 196set ( defaultBuildTypes Debug Release MinSizeRel RelWithDebInfo ) 197 198get_property ( isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG ) 199if ( isMultiConfig) 200# Multi-config generators use the first element in 201# CMAKE_CONFIGURATION_TYPES as the default build type 202list ( INSERT defaultBuildTypes 0 ${ BUILD_TYPE }) 203list ( REMOVE_DUPLICATES defaultBuildTypes ) 204set ( CMAKE_CONFIGURATION_TYPES ${ defaultBuildTypes } PARENT_SCOPE ) 205if ( CMAKE_BUILD_TYPE) 206message ( WARNING "CMAKE_BUILD_TYPE specified which is ignored on \ 207multi-configuration generators. Defaulting to ${ BUILD_TYPE } build type." ) 208endif () 209else () 210set_property ( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS " ${ defaultBuildTypes } " ) 211if ( NOT CMAKE_BUILD_TYPE) 212message ( STATUS "CMAKE_BUILD_TYPE not specified, default is ' ${ BUILD_TYPE } '" ) 213set ( CMAKE_BUILD_TYPE ${ BUILD_TYPE } CACHE STRING "Choose the type of build" FORCE ) 214else () 215message ( STATUS "CMAKE_BUILD_TYPE= ${ CMAKE_BUILD_TYPE } " ) 216endif () 217endif () 218endfunction () 219 220# Check if a module is available in Lua 221function ( check_lua_module LUA_PRG_PATH MODULE RESULT_VAR ) 222execute_process ( COMMAND ${ LUA_PRG_PATH } -l " ${ MODULE } " -e "" 223RESULT_VARIABLE module_missing ) 224if ( module_missing) 225set ( $ { RESULT_VAR } FALSE PARENT_SCOPE ) 226else () 227set ( $ { RESULT_VAR } TRUE PARENT_SCOPE ) 228endif () 229endfunction ()