Make add_defaults() return a new dict rather than mutating one.
[cdist.git] / cdist
diff --git a/cdist b/cdist
index a7af8ad0ab832719aa281047b55eed4db5dabdee..4e10752b95f6aefba0334907d25d07e60e5b5c38 100755 (executable)
--- a/cdist
+++ b/cdist
@@ -404,7 +404,7 @@ class Target:
 
     """
 
-    def __init__(self, platform, directory=None):
+    def __init__(self, platform, directory=None, dependencies_only=False):
         """
         platform -- platform string (e.g. 'windows', 'linux', 'osx')
         directory -- directory to work in; if None we will use a temporary directory
@@ -420,6 +420,7 @@ class Target:
         # True to build our dependencies ourselves; False if this is taken care
         # of in some other way
         self.build_dependencies = True
+        self.dependencies_only = dependencies_only
 
         if directory is None:
             try:
@@ -466,7 +467,8 @@ class Target:
         tree = globals.trees.get(project, checkout, self)
         if self.build_dependencies:
             tree.build_dependencies(options)
-        tree.build(options, for_package=for_package)
+        if not self.dependencies_only:
+            tree.build(options, for_package=for_package)
         return tree
 
     def test(self, project, checkout, target, test, options):
@@ -583,16 +585,17 @@ class WindowsDockerTarget(DockerTarget):
     """
     This target exposes the following additional API:
 
-    version: Windows version ('xp' or None)
     bits: bitness of Windows (32 or 64)
     name: name of our target e.g. x86_64-w64-mingw32.shared
     environment_prefix: path to Windows environment for the appropriate target (libraries and some tools)
     tool_path: path to 32- and 64-bit tools
     """
-    def __init__(self, windows_version, bits, directory, environment_version):
+    def __init__(self, bits, directory, environment_version):
         super(WindowsDockerTarget, self).__init__('windows', directory)
-        self.version = windows_version
         self.bits = bits
+        # This was used to differentiate "normal" Windows from XP, and is no longer important,
+        # but old cscripts still look for it
+        self.version = None
 
         self.tool_path = '%s/usr/bin' % config.get('mxe_prefix')
         if self.bits == 32:
@@ -657,14 +660,12 @@ class WindowsNativeTarget(Target):
     """
     This target exposes the following additional API:
 
-    version: Windows version ('xp' or None)
     bits: bitness of Windows (32 or 64)
     name: name of our target e.g. x86_64-w64-mingw32.shared
     environment_prefix: path to Windows environment for the appropriate target (libraries and some tools)
     """
     def __init__(self, directory):
         super().__init__('windows', directory)
-        self.version = None
         self.bits = 64
 
         self.environment_prefix = config.get('windows_native_environmnet_prefix')
@@ -724,11 +725,9 @@ class AppImageTarget(LinuxTarget):
         self.privileged = True
 
 
-class FlatpakTarget(LinuxTarget):
+class FlatpakTarget(Target):
     def __init__(self, project, checkout, work):
-        super(FlatpakTarget, self).__init__('ubuntu', '22.04', 64, work)
-        self.platform = 'flatpak'
-        self.privileged = True
+        super(FlatpakTarget, self).__init__('flatpak')
         self.build_dependencies = False
         self.project = project
         self.checkout = checkout
@@ -738,6 +737,10 @@ class FlatpakTarget(LinuxTarget):
         if config.has('flatpak_state_dir'):
             self.mount(config.get('flatpak_state_dir'))
 
+    def command(self, c):
+        log_normal('host -> %s' % c)
+        command('%s %s' % (self.variables_string(), c))
+
     def setup(self):
         super().setup()
         globals.trees.get(self.project, self.checkout, self).checkout_dependencies()
@@ -882,6 +885,11 @@ class OSXUniversalTarget(OSXTarget):
 
         super().package(project, checkout, output_dir, options, notarize)
 
+    @Target.ccache.setter
+    def ccache(self, v):
+        for target in self.sub_targets:
+            target.ccache = v
+
 
 class SourceTarget(Target):
     """Build a source .tar.bz2 and .zst"""
@@ -907,6 +915,28 @@ class SourceTarget(Target):
             zstd = os.path.abspath('%s-%s.tar.zst' % (name, tree.version))
             copyfile(zstd, os.path.join(output_dir, os.path.basename(devel_to_git(tree.commit, zstd))))
 
+
+class LocalTarget(Target):
+    """Build on the local machine with its environment"""
+    def __init__(self, work, dependencies_only=False):
+        super(LocalTarget, self).__init__('linux', work, dependencies_only=dependencies_only)
+        # Hack around ffmpeg.git which sees that the target isn't windows/osx and then assumes
+        # distro will be there.
+        self.distro = None
+        self.detail = None
+        self.bits = 64
+        self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
+        self.append_with_colon('LD_LIBRARY_PATH', '%s/lib' % self.directory)
+        self.set('CXXFLAGS', '-I%s/include' % self.directory)
+        self.set('LINKFLAGS', '-L%s/lib' % self.directory)
+
+    def command(self, c):
+        log_normal('host -> %s' % c)
+        command('%s %s' % (self.variables_string(), c))
+
+    def cleanup(self):
+        rmtree(self.directory)
+
 # @param s Target string:
 #       windows-{32,64}
 #    or ubuntu-version-{32,64}
@@ -925,13 +955,10 @@ def target_factory(args):
         x = s.split('-')
         if platform.system() == "Windows":
             target = WindowsNativeTarget(args.work)
+        elif len(x) == 2:
+            target = WindowsDockerTarget(int(x[1]), args.work, args.environment_version)
         else:
-            if len(x) == 2:
-                target = WindowsDockerTarget(None, int(x[1]), args.work, args.environment_version)
-            elif len(x) == 3:
-                target = WindowsDockerTarget(x[1], int(x[2]), args.work, args.environment_version)
-            else:
-                raise Error("Bad Windows target name `%s'")
+            raise Error("Bad Windows target name `%s'")
     elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-') or s.startswith('fedora-') or s.startswith('mageia-'):
         p = s.split('-')
         if len(p) != 3:
@@ -956,6 +983,8 @@ def target_factory(args):
         target = FlatpakTarget(args.project, args.checkout, args.work)
     elif s == 'appimage':
         target = AppImageTarget(args.work)
+    elif s == 'local':
+        target = LocalTarget(args.work, args.dependencies_only)
 
     if target is None:
         raise Error("Bad target `%s'" % s)
@@ -1061,7 +1090,8 @@ class Tree:
             return self.cscript[function](self.target, *args)
 
     def add_defaults(self, options):
-        """Add the defaults from self into a dict options"""
+        """Add the defaults from self into a dict options and returns a new dict"""
+        new_options = copy.copy(options)
         if 'option_defaults' in self.cscript:
             from_cscript = self.cscript['option_defaults']
             if isinstance(from_cscript, dict):
@@ -1070,8 +1100,9 @@ class Tree:
                 log_normal("Deprecated cscript option_defaults method; replace with a dict")
                 defaults_dict = from_cscript()
             for k, v in defaults_dict.items():
-                if not k in options:
-                    options[k] = v
+                if not k in new_options:
+                    new_options[k] = v
+        return new_options
 
     def dependencies(self, options):
         """
@@ -1083,9 +1114,7 @@ class Tree:
             return
 
         if len(inspect.getfullargspec(self.cscript['dependencies']).args) == 2:
-            self_options = copy.copy(options)
-            self.add_defaults(self_options)
-            deps = self.call('dependencies', self_options)
+            deps = self.call('dependencies', self.add_defaults(options))
         else:
             log_normal("Deprecated cscript dependencies() method with no options parameter")
             deps = self.call('dependencies')
@@ -1098,7 +1127,7 @@ class Tree:
             dep_options = d[2] if len(d) > 2 else {}
             for i in dep.dependencies(dep_options):
                 yield i
-            yield (dep, dep_options, self)
+            yield (dep, dep_options)
 
     def checkout_dependencies(self, options={}):
         for i in self.dependencies(options):
@@ -1120,15 +1149,12 @@ class Tree:
 
         variables = copy.copy(self.target.variables)
 
-        options = copy.copy(options)
-        self.add_defaults(options)
-
         if not globals.dry_run:
             num_args = len(inspect.getfullargspec(self.cscript['build']).args)
             if num_args == 3:
-                self.call('build', options, for_package)
+                self.call('build', self.add_defaults(options), for_package)
             elif num_args == 2:
-                self.call('build', options)
+                self.call('build', self.add_defaults(options))
             else:
                 self.call('build')
 
@@ -1161,6 +1187,7 @@ def main():
 
     subparsers = parser.add_subparsers(help='command to run', dest='command')
     parser_build = subparsers.add_parser("build", help="build project")
+    parser_build.add_argument('--dependencies-only', help='only build dependencies', action='store_true')
     parser_package = subparsers.add_parser("package", help="build and package project")
     parser_package.add_argument('--no-notarize', help='do not notarize .dmg packages', action='store_true')
     parser_release = subparsers.add_parser("release", help="release a project using its next version number (adding a tag)")
@@ -1377,19 +1404,6 @@ def main():
             shutil.copytree('.', args.output)
         target.cleanup()
 
-    elif args.command == 'dependencies':
-        if args.target is None:
-            raise Error('you must specify -t or --target')
-        if args.checkout is None:
-            raise Error('you must specify -c or --checkout')
-
-        target = target_factory(args)
-        tree = globals.trees.get(args.project, args.checkout, target)
-        print("strict digraph {")
-        for d in list(tree.dependencies({})):
-            print("%s -> %s;" % (d[2].name.replace("-", "-"), d[0].name.replace("-", "_")))
-        print("}")
-
     elif args.command == 'notarize':
         if args.dmgs is None:
             raise Error('you must specify ---dmgs')