summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorAnderson Torres <torres.anderson.85@protonmail.com>2023-09-20 20:41:57 -0300
committerAnderson Torres <torres.anderson.85@protonmail.com>2023-09-27 20:49:38 -0300
commitcc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58 (patch)
tree7e4c59b957b793030dac380ab0317423ec10eb0a /lib
parentae74cf5db83c53b7e4dd179f0fa7b6440b382ae4 (diff)
downloadnixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.tar
nixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.tar.gz
nixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.tar.bz2
nixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.tar.lz
nixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.tar.xz
nixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.tar.zst
nixpkgs-cc3383d12b25f8e1f2b23fdb098a9cb1b22dfa58.zip
lib: add cmakeOptionType, cmakeBool and cmakeFeature
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix1
-rw-r--r--lib/strings.nix58
2 files changed, 59 insertions, 0 deletions
diff --git a/lib/default.nix b/lib/default.nix
index e4bf45aac3b..169f013191b 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 df891c89988..d7642ce10fa 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<feature>:<type>=<value>" 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<condition>={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<feature>:STRING=<value> 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<feature>=<value> string that can be passed to typical Meson
      invocations.