Skip to content

Commit

Permalink
[Build] Use Java script instead of JavaScript to collect native sources
Browse files Browse the repository at this point in the history
to replace ANT tasks with JavaScript in Jenkins pipeline.
And stash native sources not-zipped to save the zip and unzip steps.

Furthermore move all remaining ANT tasks to run a native build in a
local Maven for the running platform completely to the
maven-antrun-plugin configuration.

Part of
- eclipse-platform#513
- eclipse-platform#626
  • Loading branch information
HannesWell committed Jan 13, 2024
1 parent 968c813 commit 4e2b7d0
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 150 deletions.
31 changes: 14 additions & 17 deletions Jenkinsfile
Expand Up @@ -64,6 +64,7 @@ pipeline {
agent {
label 'centos-latest'
}
// Define tools only in this stage to not interfere with default environemts of SWT-natives build-agents
environment {
MAVEN_OPTS = "-Xmx4G"
PR_VALIDATION_BUILD = "true"
Expand Down Expand Up @@ -128,17 +129,19 @@ pipeline {
}
stages {
stage("Collect SWT-native's sources") {
tools {
jdk 'openjdk-jdk17-latest'
}
steps {
dir('eclipse.platform.swt/binaries'){
withAnt(installation: 'apache-ant-latest', jdk: 'openjdk-jdk11-latest') { // nashorn javascript-engine required in ant-scripts
sh '''
pfSpec=(${PLATFORM//"."/ })
ant -f binaries-parent/build.xml copy_library_src_and_create_zip -Dws=${pfSpec[0]} -Dos=${pfSpec[1]} -Darch=${pfSpec[2]}
'''
}
dir("org.eclipse.swt.${PLATFORM}/tmpdir") {
stash name:"swt.binaries.sources.${PLATFORM}", includes: "org.eclipse.swt.${PLATFORM}.master.zip"
}
dir('eclipse.platform.swt/bundles/org.eclipse.swt') {
sh '''
pfSpec=(${PLATFORM//"."/ })
nativeBuildDir='${WORKSPACE}/eclipse.platform.swt/binaries/org.eclipse.swt.${PLATFORM}/target/natives-build-temp'
java -Dws=${pfSpec[0]} -Darch=${pfSpec[2]} build-scripts/CollectSources.java -nativeSources '${nativeBuildDir}'
'''
}
dir("eclipse.platform.swt/binaries/org.eclipse.swt.${PLATFORM}/target/natives-build-temp") {
stash(name:"swt.binaries.sources.${PLATFORM}")
}
}
}
Expand Down Expand Up @@ -167,20 +170,15 @@ pipeline {
withEnv(['MODEL=' + arch, "OUTPUT_DIR=${WORKSPACE}/libs", "SWT_JAVA_HOME=${WORKSPACE}/jdk.resources"]) {
if (isUnix()){
sh '''
unzip -aa org.eclipse.swt.${PLATFORM}.master.zip
rm org.eclipse.swt.${PLATFORM}.master.zip
mkdir libs
sh build.sh install
ls -1R libs
'''
} else {
//TODO: check if we can use sh here as well? or at least remove the env?
withEnv(['PATH=C:\\tools\\cygwin\\bin;' + env.PATH]) {
bat '''
unzip org.eclipse.swt.%PLATFORM%.master.zip
rm org.eclipse.swt.%PLATFORM%.master.zip
mkdir libs
cmd /c build.bat x86_64 all install
ls -1R libs
'''
Expand Down Expand Up @@ -278,7 +276,6 @@ pipeline {
}
stage('Build') {
tools {
// Define tools only in this stage to not interfere with default environemts of SWT-natives build-agents
jdk 'openjdk-jdk17-latest'
maven 'apache-maven-latest'
}
Expand Down
54 changes: 38 additions & 16 deletions binaries/pom.xml
Expand Up @@ -48,6 +48,9 @@
<exists>META-INF/MANIFEST.MF</exists>
</file>
</activation>
<properties>
<swtMainProject>${project.basedir}/../../bundles/org.eclipse.swt</swtMainProject>
</properties>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -104,7 +107,7 @@
</goals>
<configuration>
<target> <!-- Read git qualifier of 'org.eclipse.swt' project. -->
<loadfile property="swtBuildQualifier" srcFile="../../bundles/org.eclipse.swt/target/swtBuildQualifier.txt"/>
<loadfile property="swtBuildQualifier" srcFile="${swtMainProject}/target/swtBuildQualifier.txt"/>
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
Expand All @@ -123,19 +126,48 @@
</configuration>
</execution>
<execution>
<id>natives</id>
<id>build-native-binaries</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- See https://stackoverflow.com/a/53227117 and http://ant-contrib.sourceforge.net/tasks/tasks/index.html -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<if>
<equals arg1="${native}" arg2="${ws}.${os}.${arch}" />
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<if><equals arg1="${native}" arg2="${ws}.${os}.${arch}"/>
<then>
<ant antfile="../binaries-parent/build.xml" target="build_libraries" />
<property name="output_dir" value="${project.basedir}"/>
<property name="build_dir" value="${project.build.directory}/natives-build-temp"/>

<exec executable="java" dir="${swtMainProject}" failonerror="true">
<arg line="-Dws=${ws} build-scripts/CollectSources.java -nativeSources '${build_dir}'"/>
</exec>

<property name="SWT_JAVA_HOME" value="${java.home}"/><!-- Not overwritten if already set, e.g. as CLI argument -->
<echo>Compile SWT natives against headers and libraries from JDK: ${SWT_JAVA_HOME}</echo>
<if><equals arg1="${ws}" arg2="win32" />
<then>
<exec dir="${build_dir}" executable="${build_dir}/build.bat" failonerror="true">
<env key="SWT_JAVA_HOME" value="${SWT_JAVA_HOME}"/>
<env key="OUTPUT_DIR" value="${output_dir}"/>
<arg line="${targets}"/>
<arg line="clean"/>
</exec>
</then>
<else>
<property name="gtk_version" value="3.0" />
<exec dir="${build_dir}" executable="sh" failonerror="true">
<arg line="build.sh"/>
<env key="SWT_JAVA_HOME" value="${SWT_JAVA_HOME}"/>
<env key="OUTPUT_DIR" value="${output_dir}"/>
<env key="GTK_VERSION" value="${gtk_version}"/>
<env key="MODEL" value="${arch}"/>
<arg line="${targets}"/>
<arg line="clean"/>
</exec>
</else>
</if>
</then>
</if>
</target>
Expand All @@ -156,21 +188,11 @@
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino-runtime</artifactId>
<version>1.7.14</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.14</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-apache-bsf</artifactId>
<version>1.10.14</version>
</dependency>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
Expand Down
1 change: 1 addition & 0 deletions bundles/org.eclipse.swt/.classpath_cocoa_aarch64
Expand Up @@ -29,5 +29,6 @@
<classpathentry kind="src" path="Eclipse SWT WebKit/cocoa"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/cocoa"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
<classpathentry kind="src" output="bin_build" path="build-scripts"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions bundles/org.eclipse.swt/.classpath_cocoa_x86_64
Expand Up @@ -29,5 +29,6 @@
<classpathentry kind="src" path="Eclipse SWT WebKit/cocoa"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/cocoa"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
<classpathentry kind="src" output="bin_build" path="build-scripts"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions bundles/org.eclipse.swt/.classpath_gtk
Expand Up @@ -31,5 +31,6 @@
<classpathentry kind="src" path="Eclipse SWT OpenGL/glx"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
<classpathentry kind="src" path="Eclipse SWT WebKit/gtk"/>
<classpathentry kind="src" output="bin_build" path="build-scripts"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions bundles/org.eclipse.swt/.classpath_win32
Expand Up @@ -25,5 +25,6 @@
<classpathentry kind="src" path="Eclipse SWT Browser/win32"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/win32"/>
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
<classpathentry kind="src" output="bin_build" path="build-scripts"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions bundles/org.eclipse.swt/.gitignore
Expand Up @@ -2,3 +2,4 @@
ws/
.build64/
.buildas/
/bin_build/
87 changes: 87 additions & 0 deletions bundles/org.eclipse.swt/build-scripts/CollectSources.java
@@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright (c) 2024, 2024 Hannes Wellmann and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Hannes Wellmann - initial API and implementation
*******************************************************************************/
import java.io.*;
import java.nio.file.*;
import java.util.*;

/**
* Script to collect the SWT native sources for the specified native fragment.
* <p>
* In order to be able to launch this directly as source script <b>only
* reference standard java classes!</b>
* </p>
*/
public class CollectSources {

private record ScriptEnvironment(Path swtProjectRoot, Path targetDirectory, String ws, String arch) {
private static ScriptEnvironment read(String[] args) {
if (args.length != 2) {
throw new IllegalArgumentException("task and target directory must be defined as only argument");
}
Path swtProjectRoot = Path.of("").toAbsolutePath();
if (!swtProjectRoot.endsWith(Path.of("bundles/org.eclipse.swt"))) { // Consistency check
throw new IllegalStateException("Sript must be excuted from org.eclipse.swt project");
}
Path targetDirectory = Path.of(args[1]);
String ws = System.getProperty("ws");
String arch = System.getProperty("arch");
return new ScriptEnvironment(swtProjectRoot, targetDirectory, ws, arch);
}
}

public static void main(String[] args) throws Exception {
ScriptEnvironment env = ScriptEnvironment.read(args);
switch (args[0]) {
case "-nativeSources":
collectNativeSources(env);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + args[0]);
}
}

private static void collectNativeSources(ScriptEnvironment env) throws IOException {
Path props = env.swtProjectRoot.resolve("nativeSourceFolders.properties");
Map<String, String> sources = loadProperty(props);

String commonSources = sources.get("src_common");
String nativeSources = sources.get("src_" + env.ws);
List<String> allSources = Arrays.asList((commonSources + "," + nativeSources).split(","));
System.out.println("Copy " + allSources.size() + " java source folders for " + env.ws + "." + env.arch);
copySubDirectories(env.swtProjectRoot, allSources, env.targetDirectory);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map<String, String> loadProperty(Path path) throws IOException {
try (InputStream in = Files.newInputStream(path)) {
Properties props = new Properties();
props.load(in);
return (Map) props;
}
}

private static void copySubDirectories(Path root, Collection<String> allSources, Path target) throws IOException {
System.out.println("from " + root + "\nto " + target);
for (String srcPath : allSources) {
Path srcFolder = root.resolve(srcPath);
try (var files = Files.walk(srcFolder).filter(Files::isRegularFile)) {
for (Path sourceFile : (Iterable<Path>) files::iterator) {
Path targetFile = target.resolve(srcFolder.relativize(sourceFile));
Files.createDirectories(targetFile.getParent());
Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
}

0 comments on commit 4e2b7d0

Please sign in to comment.