diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index a81d08436c..0a75b38502 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -494,6 +494,8 @@ set(lisbslic3r_sources SlicingAdaptive.hpp Slicing.cpp Slicing.hpp + SourceDateEpoch.cpp + SourceDateEpoch.hpp Support/BeltFloorContext.cpp Support/BeltFloorContext.hpp Support/SupportCommon.cpp diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 1549ccafa5..deb2ed0f9f 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -45,6 +45,7 @@ #include "GCode/WipeTower.hpp" #include "GCode/WipeTower2.hpp" #include "ShortestPath.hpp" +#include "SourceDateEpoch.hpp" #include "GCode/OrderingStrategies.hpp" #include "Print.hpp" #include "Utils.hpp" @@ -68,6 +69,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +84,7 @@ #include #include #include +#include #include #include #include @@ -3591,7 +3594,11 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato m_placeholder_parser_integration.parser = print.placeholder_parser(); m_placeholder_parser_integration.parser.update_timestamp(); m_placeholder_parser_integration.parser.update_user_name(); - m_placeholder_parser_integration.context.rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + // ORCA_SOURCE_DATE_EPOCH also fixes the numbers random() draws, so that an export can be repeated. + if (const std::optional fixed_time = source_date_epoch()) + m_placeholder_parser_integration.context.rng = std::mt19937(static_cast(*fixed_time)); + else + m_placeholder_parser_integration.context.rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count()); // Enable passing global variables between PlaceholderParser invocations. m_placeholder_parser_integration.context.global_config = std::make_unique(); print.update_object_placeholders(m_placeholder_parser_integration.parser.config_writable(), ".gcode"); diff --git a/src/libslic3r/PlaceholderParser.cpp b/src/libslic3r/PlaceholderParser.cpp index a0478799f1..138125662b 100644 --- a/src/libslic3r/PlaceholderParser.cpp +++ b/src/libslic3r/PlaceholderParser.cpp @@ -4,6 +4,7 @@ #include "Flow.hpp" #include "PrintConfig.hpp" #include "Point.hpp" +#include "SourceDateEpoch.hpp" #include "Utils.hpp" #include "I18N.hpp" #include "format.hpp" @@ -41,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -119,9 +121,12 @@ PlaceholderParser::PlaceholderParser(const DynamicConfig *external_config) : m_e void PlaceholderParser::update_timestamp(DynamicConfig &config) { - time_t rawtime; - time(&rawtime); - struct tm* timeinfo = localtime(&rawtime); + // ORCA_SOURCE_DATE_EPOCH replaces the clock, read as UTC, so that an export can be repeated. + const std::optional fixed_time = source_date_epoch(); + time_t rawtime = fixed_time.value_or(0); + if (!fixed_time) + time(&rawtime); + struct tm* timeinfo = fixed_time ? gmtime(&rawtime) : localtime(&rawtime); { std::ostringstream ss; diff --git a/src/libslic3r/SourceDateEpoch.cpp b/src/libslic3r/SourceDateEpoch.cpp new file mode 100644 index 0000000000..50f3d4485a --- /dev/null +++ b/src/libslic3r/SourceDateEpoch.cpp @@ -0,0 +1,33 @@ +#include "SourceDateEpoch.hpp" + +#include +#include +#include +#include +#include +#include + +#include + +namespace Slic3r { + +std::optional source_date_epoch() +{ + // The last second every platform's gmtime() accepts: Windows stops at the end of the year 3000. + // A later time would make gmtime() return null. + constexpr std::int64_t last_second = 32535215999; + + // Boost.Nowide's getenv, not std::getenv: on Windows it reads the process environment, so it also + // sees a value set after start-up, which the C runtime's copy does not. + const char *text = boost::nowide::getenv("ORCA_SOURCE_DATE_EPOCH"); + if (text == nullptr) + return std::nullopt; + const char *end = text + std::strlen(text); + std::int64_t seconds = 0; + const auto [parsed_to, error] = std::from_chars(text, end, seconds); + if (error != std::errc() || parsed_to == text || parsed_to != end || seconds < 0 || seconds > last_second) + return std::nullopt; + return static_cast(seconds); +} + +} // namespace Slic3r diff --git a/src/libslic3r/SourceDateEpoch.hpp b/src/libslic3r/SourceDateEpoch.hpp new file mode 100644 index 0000000000..08f49f54bc --- /dev/null +++ b/src/libslic3r/SourceDateEpoch.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +namespace Slic3r { + +// The time to use in place of the current time: the ORCA_SOURCE_DATE_EPOCH environment variable, +// a whole number of seconds since the Unix epoch. Unset, empty or anything else gives nullopt. +// +// With it set, custom G-code prints that time (in UTC) and random() draws the same numbers on every +// export, so two slices of the same input give the same G-code and can be compared. It is not the +// standard SOURCE_DATE_EPOCH, which packaging shells export for every program they start. +std::optional source_date_epoch(); + +} // namespace Slic3r diff --git a/tests/fff_print/test_gcode.cpp b/tests/fff_print/test_gcode.cpp index 027ee72837..53f3ecc4eb 100644 --- a/tests/fff_print/test_gcode.cpp +++ b/tests/fff_print/test_gcode.cpp @@ -90,6 +90,33 @@ TEST_CASE("Kobra 2 layer-change templates do not emit numeric commands", "[GCode }); } +TEST_CASE("ORCA_SOURCE_DATE_EPOCH makes custom G-code print the same time and random numbers on every export", "[GCode]") +{ + const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "1000000000"); // 2001-09-09 01:46:40 UTC + const auto config = Test::multifilament_config(1, { + {"layer_height", "0.2"}, + {"first_layer_height", "0.2"}, + {"layer_change_gcode", "; clock [timestamp] roll {random(0, 1000000)}"}, + }); + const auto clock_lines = [&config]() { + std::vector lines; + // A 2 mm cube at 0.2 mm layers changes layer several times, so random() is drawn several times. + std::istringstream gcode(Test::slice({Test::cube(2.)}, config)); + for (std::string line; std::getline(gcode, line);) + if (line.rfind("; clock ", 0) == 0) + lines.push_back(line); + return lines; + }; + + const std::vector first = clock_lines(); + REQUIRE(first.size() > 1); + for (const std::string &line : first) + CHECK(line.rfind("; clock 20010909-014640 roll ", 0) == 0); + // The sequence still advances from layer to layer. Only its starting point is fixed. + CHECK(std::set(first.begin(), first.end()).size() > 1); + CHECK(clock_lines() == first); +} + TEST_CASE("Klipper object labels name each copy without the characters Klipper cannot parse", "[GCode]") { const auto [name, label] = GENERATE(table({ diff --git a/tests/libslic3r/test_placeholder_parser.cpp b/tests/libslic3r/test_placeholder_parser.cpp index ab5f44d76e..1d59b7702d 100644 --- a/tests/libslic3r/test_placeholder_parser.cpp +++ b/tests/libslic3r/test_placeholder_parser.cpp @@ -1,11 +1,16 @@ #include +#include +#include #include #include #include "libslic3r/Config.hpp" +#include "libslic3r/SourceDateEpoch.hpp" +#include "test_utils.hpp" #include #include #include +#include #include #include #include "libslic3r/PlaceholderParser.hpp" @@ -400,6 +405,36 @@ SCENARIO("Placeholder parser names in branches that are not taken", "[Placeholde } } +TEST_CASE("ORCA_SOURCE_DATE_EPOCH is read as a whole number of seconds", "[PlaceholderParser]") { + const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "1000000000"); + CHECK(source_date_epoch() == std::optional(1000000000)); +} + +TEST_CASE("ORCA_SOURCE_DATE_EPOCH is read up to the end of the year 3000", "[PlaceholderParser]") { + const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "32535215999"); // 3000-12-31 23:59:59 UTC + PlaceholderParser parser; + CHECK(parser.process("[timestamp]") == "30001231-235959"); +} + +TEST_CASE("ORCA_SOURCE_DATE_EPOCH is ignored when it is not a whole number of seconds the clock can hold", "[PlaceholderParser]") { + // The last two are later than any platform's gmtime() accepts. + const std::string value = GENERATE("", "abc", "-5", "12x", " 12", "1e9", "32535216000", "9223372036854775807"); + INFO("value: \"" << value << "\""); + const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", value); + CHECK_FALSE(source_date_epoch().has_value()); +} + +TEST_CASE("ORCA_SOURCE_DATE_EPOCH is ignored when it is not set", "[PlaceholderParser]") { + const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", std::nullopt); + CHECK_FALSE(source_date_epoch().has_value()); +} + +TEST_CASE("ORCA_SOURCE_DATE_EPOCH replaces the clock in the time variables, read as UTC", "[PlaceholderParser]") { + const ScopedEnvVar epoch("ORCA_SOURCE_DATE_EPOCH", "1000000000"); // 2001-09-09 01:46:40 UTC + PlaceholderParser parser; // reads the clock when it is built + CHECK(parser.process("[timestamp] [year]-[month]-[day] [hour]:[minute]:[second]") == "20010909-014640 2001-9-9 1:46:40"); +} + TEST_CASE("Placeholder parser deferred values can be printed but not computed with", "[PlaceholderParser]") { PlaceholderParser parser; parser.set_deferred("total_weight", "@TOTAL_WEIGHT@");