diff --git a/lib/default.nix b/lib/default.nix index e4bf45aac3b6..169f013191b8 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -106,6 +106,7 @@ let upperChars toLower toUpper addContextFrom splitString removePrefix removeSuffix versionOlder versionAtLeast getName getVersion + cmakeOptionType cmakeBool cmakeFeature mesonOption mesonBool mesonEnable nameFromURL enableFeature enableFeatureAs withFeature withFeatureAs fixedWidthString fixedWidthNumber diff --git a/lib/strings.nix b/lib/strings.nix index df891c899887..d7642ce10faf 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -741,6 +741,64 @@ rec { name = head (splitString sep filename); in assert name != filename; name; + /* Create a "-D:=" string that can be passed to typical + CMake invocations. + + Type: cmakeOptionType :: string -> string -> string -> string + + @param feature The feature to be set + @param type The type of the feature to be set, as described in + https://cmake.org/cmake/help/latest/command/set.html + the possible values (case insensitive) are: + BOOL FILEPATH PATH STRING INTERNAL + @param value The desired value + + Example: + cmakeOptionType "string" "ENGINE" "sdl2" + => "-DENGINE:STRING=sdl2" + */ + cmakeOptionType = type: feature: value: + assert (lib.elem (lib.toUpper type) + [ "BOOL" "FILEPATH" "PATH" "STRING" "INTERNAL" ]); + assert (lib.isString feature); + assert (lib.isString value); + "-D${feature}:${lib.toUpper type}=${value}"; + + /* Create a -D={TRUE,FALSE} string that can be passed to typical + CMake invocations. + + Type: cmakeBool :: string -> bool -> string + + @param condition The condition to be made true or false + @param flag The controlling flag of the condition + + Example: + cmakeBool "ENABLE_STATIC_LIBS" false + => "-DENABLESTATIC_LIBS:BOOL=FALSE" + */ + cmakeBool = condition: flag: + assert (lib.isString condition); + assert (lib.isBool flag); + cmakeOptionType "bool" condition (lib.toUpper (lib.boolToString flag)); + + /* Create a -D:STRING= string that can be passed to typical + CMake invocations. + This is the most typical usage, so it deserves a special case. + + Type: cmakeFeature :: string -> string -> string + + @param condition The condition to be made true or false + @param flag The controlling flag of the condition + + Example: + cmakeFeature "MODULES" "badblock" + => "-DMODULES:STRING=badblock" + */ + cmakeFeature = feature: value: + assert (lib.isString feature); + assert (lib.isString value); + cmakeOptionType "string" feature value; + /* Create a -D= string that can be passed to typical Meson invocations.