Optionally pass options into test().
[cdist.git] / cdist
diff --git a/cdist b/cdist
index b53a8a267b7ba8b5928c55bbb662488e8bc6d874..ffcce29403309058a2a284463f14282fdc12020c 100755 (executable)
--- a/cdist
+++ b/cdist
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 #    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
 #
@@ -103,6 +103,7 @@ class Config:
     def __init__(self):
         self.options = [ Option('mxe_prefix'),
                          Option('git_prefix'),
+                         Option('git_reference'),
                          Option('osx_environment_prefix'),
                          Option('osx_sdk_prefix'),
                          Option('osx_sdk'),
@@ -445,7 +446,7 @@ class Target(object):
             self.set('CCACHE_BASEDIR', os.path.realpath(self.directory))
             self.set('CCACHE_NOHASHDIR', '')
         else:
-            self.directory = directory
+            self.directory = os.path.realpath(directory)
             self.rmdir = False
 
 
@@ -479,7 +480,13 @@ class Target(object):
         if self.build_dependencies:
             tree.build_dependencies(options)
         tree.build(options)
-        return tree.call('test', test)
+
+        tree.add_defaults(options)
+        if len(inspect.getfullargspec(tree.cscript['test']).args) == 3:
+            return tree.call('test', options, test)
+        else:
+            log_normal('Deprecated cscript test() method with no options parameter')
+            return tree.call('test', test)
 
     def set(self, a, b):
         self.variables[a] = b
@@ -541,14 +548,19 @@ class DockerTarget(Target):
             return ''
         return '-u %s' % getpass.getuser()
 
+    def _mount_option(self, d):
+        return '-v %s:%s ' % (os.path.realpath(d), os.path.realpath(d))
+
     def setup(self):
-        opts = '-v %s:%s ' % (self.directory, self.directory)
+        opts = self._mount_option(self.directory)
         for m in self.mounts:
-            opts += '-v %s:%s ' % (m, m)
+            opts += self._mount_option(m)
+        if config.has('git_reference'):
+            opts += self._mount_option(config.get('git_reference'))
         if self.privileged:
             opts += '--privileged=true '
         if self.ccache:
-            opts += "-e CCACHE_DIR=/ccache --volumes-from ccache-%s" % self.image
+            opts += "-e CCACHE_DIR=/ccache/%s-%d --mount source=ccache,target=/ccache" % (self.image, os.getuid())
 
         tag = self.image
         if config.has('docker_hub_repository'):
@@ -621,8 +633,6 @@ class WindowsTarget(DockerTarget):
         self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.environment_prefix)
         self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
         self.set('PATH', '%s/bin:%s:%s' % (self.environment_prefix, self.tool_path, os.environ['PATH']))
-        self.set('CC', '%s-gcc' % self.name)
-        self.set('CXX', '%s-g++' % self.name)
         self.set('LD', '%s-ld' % self.name)
         self.set('RANLIB', '%s-ranlib' % self.name)
         self.set('WINRC', '%s-windres' % self.name)
@@ -637,6 +647,15 @@ class WindowsTarget(DockerTarget):
         if environment_version is not None:
             self.image += '_%s' % environment_version
 
+    def setup(self):
+        super().setup()
+        if self.ccache:
+            self.set('CC', '"ccache %s-gcc"' % self.name)
+            self.set('CXX', '"ccache %s-g++"' % self.name)
+        else:
+            self.set('CC', '%s-gcc' % self.name)
+            self.set('CXX', '%s-g++' % self.name)
+
     @property
     def library_prefix(self):
         log_normal('Deprecated property library_prefix: use environment_prefix')
@@ -901,7 +920,11 @@ class Tree(object):
         if globals.quiet:
             flags = '-q'
             redirect = '>/dev/null'
-        command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.directory, self.name))
+        if config.has('git_reference'):
+            ref = '--reference-if-able %s/%s.git' % (config.get('git_reference'), self.name)
+        else:
+            ref = ''
+        command('git clone %s %s %s/%s.git %s/src/%s' % (flags, ref, config.get('git_prefix'), self.name, target.directory, self.name))
         os.chdir('%s/src/%s' % (target.directory, self.name))
 
         spec = self.specifier
@@ -917,9 +940,19 @@ class Tree(object):
         exec(open('%s/cscript' % proj).read(), self.cscript)
 
         # cscript can include submodules = False to stop submodules being fetched
-        if not 'submodules' in self.cscript or self.cscript['submodules'] == True:
-            command('git submodule init --quiet')
-            command('git submodule update --quiet')
+        if (not 'submodules' in self.cscript or self.cscript['submodules'] == True) and os.path.exists('.gitmodules'):
+            command('git submodule --quiet init')
+            paths = command_and_read('git config --file .gitmodules --get-regexp path')
+            urls = command_and_read('git config --file .gitmodules --get-regexp url')
+            for path, url in zip(paths, urls):
+                path = path.split(' ')[1]
+                url = url.split(' ')[1]
+            ref = ''
+            if config.has('git_reference'):
+                ref_path = os.path.join(config.get('git_reference'), os.path.basename(url))
+                if os.path.exists(ref_path):
+                    ref = '--reference %s' % ref_path
+            command('git submodule --quiet update %s %s' % (ref, path))
 
         if os.path.exists('%s/wscript' % proj):
             v = read_wscript_variable(proj, "VERSION");