Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reproducibility for the release builds #18258

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
babelPluginPDFJSPreprocessor,
preprocessPDFJSCode,
} from "./external/builder/babel-plugin-pdfjs-preprocessor.mjs";
import { exec, spawn, spawnSync } from "child_process";
import { exec, execSync, spawn, spawnSync } from "child_process";
import autoprefixer from "autoprefixer";
import babel from "@babel/core";
import crypto from "crypto";
Expand Down Expand Up @@ -1682,13 +1682,13 @@ gulp.task(
)
);

function compressPublish(targetName, dir) {
function compressPublish({ sourceDirectory, targetFile, modifiedTime }) {
return gulp
.src(dir + "**", { encoding: false })
.pipe(zip(targetName))
.src(`${sourceDirectory}**`, { encoding: false })
.pipe(zip(targetFile, { modifiedTime }))
.pipe(gulp.dest(BUILD_DIR))
.on("end", function () {
console.log("Built distribution file: " + targetName);
console.log(`Built distribution file: ${targetFile}`);
});
}

Expand All @@ -1701,15 +1701,34 @@ gulp.task(

config.stableVersion = version;

// ZIP files record the modification date of the source files, so if files
// are generated during the build process the output is not reproducible.
// To avoid this, the modification dates should be replaced with a fixed
// date, in our case the last Git commit date, so that builds from identical
// source code result in bit-by-bit identical output. The `gulp-zip` library
// supports providing a different modification date to enable reproducible
// builds. Note that the Git command below outputs the last Git commit date
// as a Unix timestamp (in seconds since epoch), but the `Date` constructor
// in JavaScript requires millisecond input, so we have to multiply by 1000.
const lastCommitTimestamp = execSync('git log --format="%at" -n 1')
.toString()
.replace("\n", "");
const lastCommitDate = new Date(parseInt(lastCommitTimestamp, 10) * 1000);

return ordered([
createStringSource(CONFIG_FILE, JSON.stringify(config, null, 2)).pipe(
gulp.dest(".")
),
compressPublish("pdfjs-" + version + "-dist.zip", GENERIC_DIR),
compressPublish(
"pdfjs-" + version + "-legacy-dist.zip",
GENERIC_LEGACY_DIR
),
compressPublish({
sourceDirectory: GENERIC_DIR,
targetFile: `pdfjs-${version}-dist.zip`,
modifiedTime: lastCommitDate,
}),
compressPublish({
sourceDirectory: GENERIC_LEGACY_DIR,
targetFile: `pdfjs-${version}-legacy-dist.zip`,
modifiedTime: lastCommitDate,
}),
]);
})
);
Expand Down