This is an automated email from the ASF dual-hosted git repository.

damjan pushed a commit to branch scons-build
in repository https://gitbox.apache.org/repos/asf/openoffice.git

commit 2e50031fc74ed283c2469a359756b8e25c8fd6b0
Author: Damjan Jovanovic <dam...@apache.org>
AuthorDate: Fri Jul 3 19:54:34 2020 +0200

    Add GoogleTest target and automated conversion to it.
    
    Patch by: me
---
 .../openoffice/gotoSCons/SConsConverter.java       | 31 +++++++++
 .../openoffice/gotoSCons/targets/BaseTarget.java   |  1 +
 .../openoffice/gotoSCons/targets/GoogleTest.java   | 77 ++++++++++++++++++++++
 .../openoffice/gotoSCons/targets/Module.java       | 32 ++++++++-
 main/site_scons/GoogleTest.py                      | 60 +++++++++++++++++
 main/site_scons/platform/aooplatform.py            |  4 ++
 main/site_scons/platform/freebsd.py                |  7 ++
 main/site_scons/platform/windows.py                |  3 +
 main/site_scons/site_init.py                       |  4 +-
 9 files changed, 216 insertions(+), 3 deletions(-)

diff --git 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/SConsConverter.java 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/SConsConverter.java
index a113613..e181fd7 100644
--- 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/SConsConverter.java
+++ 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/SConsConverter.java
@@ -30,6 +30,7 @@ import java.util.TreeSet;
 import org.apache.openoffice.gotoSCons.targets.AllLangResTarget;
 import org.apache.openoffice.gotoSCons.targets.BaseBinary;
 import org.apache.openoffice.gotoSCons.targets.Executable;
+import org.apache.openoffice.gotoSCons.targets.GoogleTest;
 import org.apache.openoffice.gotoSCons.targets.JunitTest;
 import org.apache.openoffice.gotoSCons.targets.Library;
 import org.apache.openoffice.gotoSCons.targets.Module;
@@ -71,6 +72,10 @@ public class SConsConverter {
         for (JunitTest junitTest: module.getJunitTests().values()) {
             convertJunitTest(module, junitTest);
         }
+        
+        for (GoogleTest googleTest: module.getGoogleTests().values()) {
+            convertGoogleTest(googleTest);
+        }
     }
     
     private void convertLibrary(Library library) throws Exception {
@@ -172,6 +177,32 @@ public class SConsConverter {
         out.println();
     }
     
+    private void convertGoogleTest(GoogleTest gtest) throws Exception {
+        String objectsVariable = convertObjects(gtest);
+
+        String layer = repo.getExecutableLayer(gtest.getName());
+        out.println(String.format("%s = AOOGoogleTest(", gtest.getName()));
+        out.println(String.format("    '%s',", gtest.getName()));
+        out.println(String.format("    %s", objectsVariable));
+        out.println(String.format(")"));
+        
+        if (!gtest.getLinkedLibs().isEmpty()) {
+            out.println(String.format("%s.AddLinkedLibs([", gtest.getName()));
+            boolean first = true;
+            for (String linkedLib : gtest.getLinkedLibs()) {
+                if (!first) {
+                    out.println(",");
+                }
+                out.print("    '" + linkedLib + "'");
+                first = false;
+            }
+            out.println();
+            out.println("])");
+        }
+
+        out.println();
+    }
+        
     private void convertAllLangResTarget(AllLangResTarget allLangResTarget) 
throws Exception {
         String srsVariableName = allLangResTarget.getName() + "Srs";
         if (allLangResTarget.getSrs().size() != 1) {
diff --git 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/BaseTarget.java
 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/BaseTarget.java
index 40c17cb..157eefd 100644
--- 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/BaseTarget.java
+++ 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/BaseTarget.java
@@ -38,6 +38,7 @@ public abstract class BaseTarget {
                     throw new Exception("Top-level function isn't \"eval\" but 
\"" + functionNode.function + "\"");
                 }
             } else if (child instanceof ValueNode && 
((ValueNode)child).toString().equals("ifneq ($(OOO_JUNIT_JAR),)")) {
+            } else if (child instanceof ValueNode && 
((ValueNode)child).toString().equals("ifeq ($(ENABLE_UNIT_TESTS),YES)")) {
             } else if (child instanceof ValueNode && 
((ValueNode)child).toString().equals("endif")) {
             } else {
                 throw new Exception("Top-level declaration isn't a function 
but " + child.toString());
diff --git 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/GoogleTest.java
 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/GoogleTest.java
new file mode 100644
index 0000000..f018d89
--- /dev/null
+++ 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/GoogleTest.java
@@ -0,0 +1,77 @@
+/**************************************************************
+ * 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * 
+ *************************************************************/
+
+package org.apache.openoffice.gotoSCons.targets;
+
+import java.io.File;
+import java.util.Arrays;
+import org.apache.openoffice.gotoSCons.raw.Node;
+import org.apache.openoffice.gotoSCons.raw.ValueNode;
+
+public class GoogleTest extends BaseBinary {
+
+    public GoogleTest(File filename) throws Exception {
+        super(filename);
+    }
+
+    @Override
+    protected void parseCall(Node argsNode) throws Exception {
+        if (argsNode instanceof ValueNode) {
+            String value = ((ValueNode)argsNode).value;
+            String[] tokens = value.split(",");
+            
+            String function = tokens[0].trim();
+            String[] args = Arrays.copyOfRange(tokens, 1, tokens.length);
+            
+            if (function.equals("gb_GoogleTest_GoogleTest")) {
+                parseGoogleTestGoogleTest(args);
+            } else if (function.equals("gb_GoogleTest_add_api")) {
+                parseAddApi(args);
+            } else if (function.equals("gb_GoogleTest_add_defs")) {
+                parseAddDefs(args);
+            } else if (function.equals("gb_GoogleTest_add_exception_objects")) 
{
+                parseAddExceptionObjects(args);
+            } else if 
(function.equals("gb_GoogleTest_add_noexception_objects")) {
+                parseAddNoExceptionObjects(args);
+            } else if (function.equals("gb_GoogleTest_add_linked_libs")) {
+                parseAddLinkedLibs(args);
+            } else if (function.equals("gb_GoogleTest_add_package_headers")) {
+                parseAddPackageHeaders(args);
+            } else if 
(function.equals("gb_GoogleTest_add_precompiled_header")) {
+                parseAddPrecompiledHeader(args);
+            } else if (function.equals("gb_GoogleTest_set_include")) {
+                parseSetInclude(args);
+            } else {
+                throw new Exception("UNHANDLED FUNCTION " + function);
+            }
+        } else {
+            throw new Exception("Call args not a value");
+        }
+
+    }
+    
+    private void parseGoogleTestGoogleTest(String[] args) throws Exception {
+        if (args.length != 1) {
+            throw new Exception("Expected 1 arg, got " + 
Arrays.toString(args));
+        }
+        this.name = args[0];
+    }
+}
diff --git 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/Module.java 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/Module.java
index 03a3074..85ed77f 100644
--- 
a/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/Module.java
+++ 
b/gotoSCons/src/main/java/org/apache/openoffice/gotoSCons/targets/Module.java
@@ -45,6 +45,7 @@ public class Module extends BaseTarget {
     private TreeSet<String> targets = new TreeSet<>();
     private Map<String, Pkg> packages = new TreeMap<>();
     private Map<String, JunitTest> junitTests = new TreeMap<>();
+    private Map<String, GoogleTest> googleTests = new TreeMap<>();
     
     public Module(File filename) throws Exception {
         this.filename = filename;
@@ -70,6 +71,8 @@ public class Module extends BaseTarget {
                 parseModuleModule(args);
             } else if (function.equals("gb_Module_add_targets")) {
                 parseModuleAddTargets(args);
+            } else if (function.equals("gb_Module_add_check_targets")) {
+                parseModuleAddCheckTargets(args);
             } else if 
(function.equals("gb_Module_add_subsequentcheck_targets")) {
                 parseModuleAddSubsequentCheckTargets(args);
             } else {
@@ -130,6 +133,29 @@ public class Module extends BaseTarget {
         }
     }
 
+    private void parseModuleAddCheckTargets(String[] args) throws Exception {
+        if (args.length < 1 || args.length > 2) {
+            throw new Exception("Expected 1-2 args, got " + 
Arrays.toString(args));
+        }
+        if (!args[0].equals(name)) {
+            throw new Exception("Module isn't " + name);
+        }
+        if (args.length == 1) {
+            return; // file list empty
+        }
+        for (String arg : Utils.spaceSeparatedTokens(args[1])) {
+            File makefile = new File(filename.getParentFile(), arg + ".mk");
+            if (arg.startsWith("GoogleTest_")) {
+                GoogleTest googleTest = new GoogleTest(makefile);
+                if (googleTests.put(arg, googleTest) != null) {
+                    throw new Exception("Duplicate add of target " + arg);
+                }
+            } else {
+                throw new Exception("Unsupported target " + arg);
+            }
+        }
+    }
+    
     private void parseModuleAddSubsequentCheckTargets(String[] args) throws 
Exception {
         if (args.length < 1 || args.length > 2) {
             throw new Exception("Expected 1-2 args, got " + 
Arrays.toString(args));
@@ -184,7 +210,11 @@ public class Module extends BaseTarget {
     public Map<String, JunitTest> getJunitTests() {
         return junitTests;
     }
-    
+
+    public Map<String, GoogleTest> getGoogleTests() {
+        return googleTests;
+    }
+
     @Override
     public String toString() {
         return "Module{" + "filename=" + filename + ", name=" + name + ", 
targets=" + targets + '}';
diff --git a/main/site_scons/GoogleTest.py b/main/site_scons/GoogleTest.py
new file mode 100644
index 0000000..4c8e385
--- /dev/null
+++ b/main/site_scons/GoogleTest.py
@@ -0,0 +1,60 @@
+#**************************************************************
+#  
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#  
+#    http://www.apache.org/licenses/LICENSE-2.0
+#  
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#  
+#**************************************************************
+
+from SCons.Script import *
+from config import soenv
+from globals import *
+import os
+import subprocess
+
+class AOOGoogleTest:
+    def __init__(self, name, objects):
+        self.env = DefaultEnvironment().Clone()
+
+        if soenv.get('ENABLE_UNIT_TESTS') == 'YES':
+            outputDir = Dir('GoogleTest/' + name)
+            logFile = File('log.xml', outputDir)
+
+            self.exe = self.env.Program(
+                File(name, outputDir),
+                objects.objects
+            )
+            self.env.Append(LINKFLAGS=platform.getGoogleTestLDFlags(soenv, 
OUTDIRLOCATION, DEBUGGING, DEBUGLEVEL))
+            self.env.Append(LIBPATH=platform.getLDPATH(soenv))
+            self.env.Append(LIBS='gtest')
+            
+            self.env.Append(ENV=os.environ)
+            self.env.Append(ENV=platform.getExecutableEnvironment(soenv))
+            self.googleTestTarget = self.env.Command(logFile, self.exe, [
+                self._run_GoogleTest
+            ])
+            AlwaysBuild(self.googleTestTarget)
+
+    @staticmethod
+    def _run_GoogleTest(target, source, env):
+        completedProcess = subprocess.run(
+            args=source[0].abspath + ' --gtest_output=xml:' + 
target[0].abspath,
+            shell=True,
+            env=env['ENV'])
+        return completedProcess.returncode
+
+    def AddLinkedLibs(self, libs):
+        self.env.Append(LIBS=libs)
diff --git a/main/site_scons/platform/aooplatform.py 
b/main/site_scons/platform/aooplatform.py
index 84cfc93..1c6ea5c 100644
--- a/main/site_scons/platform/aooplatform.py
+++ b/main/site_scons/platform/aooplatform.py
@@ -77,6 +77,10 @@ class Platform(ABC):
     @abstractmethod
     def getExecutableLDFlags(self, soenv, group, outDirLocation, debugging, 
debugLevel):
         pass
+
+    @abstractmethod
+    def getGoogleTestLDFlags(self, soenv, outDirLocation, debugging, 
debugLevel):
+        pass
     
     @abstractmethod
     def getLibraryLDFlags(self, soenv, group, outDirLocation, debugging, 
debugLevel):
diff --git a/main/site_scons/platform/freebsd.py 
b/main/site_scons/platform/freebsd.py
index d04c956..eda47ff 100644
--- a/main/site_scons/platform/freebsd.py
+++ b/main/site_scons/platform/freebsd.py
@@ -222,6 +222,13 @@ class FreeBSD(aooplatform.Platform):
         ]
         return flags
 
+    def getGoogleTestLDFlags(self, soenv, outDirLocation, debugging, 
debugLevel):
+        flags = self.getLDFlags(soenv, debugging, debugLevel)
+        flags += [
+            '-Wl,-rpath-link,' + outDirLocation
+        ]
+        return flags
+
     def getLibraryLDFlags(self, soenv, group, outDirLocation, debugging, 
debugLevel):
         flags = self.getLDFlags(soenv, debugging, debugLevel)
         flags += [ '-Wl,-z,noexecstack' ]
diff --git a/main/site_scons/platform/windows.py 
b/main/site_scons/platform/windows.py
index 7615651..5e9b10c 100644
--- a/main/site_scons/platform/windows.py
+++ b/main/site_scons/platform/windows.py
@@ -257,6 +257,9 @@ class Windows(aooplatform.Platform):
             flags += ['-SAFESEH']
         return flags
 
+    def getGoogleTestLDFlags(self, soenv, outDirLocation, debugging, 
debugLevel):
+        return self.getExecutableLDFlags(soenv, None, outDirLocation, 
debugging, debugLevel)
+
     def getLibraryLDFlags(self, soenv, group, outDirLocation, debugging, 
debugLevel):
         flags = self.getLDFlags(soenv, debugging, debugLevel)
         flags += [
diff --git a/main/site_scons/site_init.py b/main/site_scons/site_init.py
index 5e51599..b9bfd73 100644
--- a/main/site_scons/site_init.py
+++ b/main/site_scons/site_init.py
@@ -33,7 +33,7 @@ from globals import *
 env = DefaultEnvironment(
     CC = soenv['CC'],
     CXX = soenv['CXX'],
-    CPPPATH = platform.getInclude(soenv)
+    CPPPATH = platform.getIncludeStl(soenv) + platform.getInclude(soenv)
 )
 env.Append(CPPDEFINES = GLOBALDEFS)
 env.Append(CFLAGS = platform.getCFlags(soenv))
@@ -65,7 +65,7 @@ from sharedLibrary import AOOSharedLibrary
 from sharedObjects import AOOSharedObjects
 from AllLangRes import *
 from JunitTest import *
-
+from GoogleTest import *
 
 def CreateSharedLibraryEnvironment(name, group):
     env = Environment()

Reply via email to