summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TARGETS1
-rw-r--r--share/just_complete.bash2
-rw-r--r--share/man/just.1.org16
-rw-r--r--src/buildtool/main/TARGETS24
-rw-r--r--src/buildtool/main/main.cpp15
-rw-r--r--src/buildtool/main/version.cpp22
-rw-r--r--src/buildtool/main/version.hpp8
7 files changed, 86 insertions, 2 deletions
diff --git a/TARGETS b/TARGETS
index 70dafc54..c1ae7d59 100644
--- a/TARGETS
+++ b/TARGETS
@@ -12,6 +12,7 @@
, "AR"
, "ENV"
, "BUILD_STATIC_BINARY"
+ , "SOURCE_DATE_EPOCH"
]
}
, "just":
diff --git a/share/just_complete.bash b/share/just_complete.bash
index 4a3cd5af..51a7e5e3 100644
--- a/share/just_complete.bash
+++ b/share/just_complete.bash
@@ -34,7 +34,7 @@ EOF
}
_just_completion(){
- local readonly SUBCOMMANDS=(build analyse describe install-cas install rebuild -h --help)
+ local readonly SUBCOMMANDS=(build analyse describe install-cas install rebuild -h --help version)
local word=${COMP_WORDS[$COMP_CWORD]}
local prev=${COMP_WORDS[$((COMP_CWORD-1))]}
local cmd=${COMP_WORDS[1]}
diff --git a/share/man/just.1.org b/share/man/just.1.org
index 15a40b9b..f38dfc11 100644
--- a/share/man/just.1.org
+++ b/share/man/just.1.org
@@ -94,6 +94,22 @@ the multi-repository configuration file.
* SUBCOMMANDS
+** version
+
+Print on stdout a JSON object providing version information about
+the version of the tool used. This JSON object will contain at
+least the following keys.
+- ~"version"~ The version, as a list of numbers of length at least
+ 3, following the usual convention that version numbers are compared
+ lexicographically.
+- ~"suffix"~ The version suffix as a string. Generally, suffixes
+ starting with a ~+~ symbol are positive offsets to the version,
+ while suffixes starting with a ~~~ symbol are negative offsets.
+- ~"SOURCE_DATE_EPOCH"~ Either a number or ~null~. If it is a
+ number, it is the time, in seconds since the epoch, of the last
+ commit that went into this binary. It is ~null~ if that time is
+ not known (e.g., in development builds).
+
** analyse|build|install
The subcommands ~analyse~, ~build~, and ~install~ are strictly
diff --git a/src/buildtool/main/TARGETS b/src/buildtool/main/TARGETS
index 6e3d3c74..4fd16a86 100644
--- a/src/buildtool/main/TARGETS
+++ b/src/buildtool/main/TARGETS
@@ -19,6 +19,7 @@
, ["src/buildtool/build_engine/target_map", "target_cache"]
, ["src/utils/cpp", "concepts"]
, ["src/utils/cpp", "json"]
+ , "version"
]
, "stage": ["src", "buildtool", "main"]
, "link external":
@@ -27,4 +28,27 @@
, "then": ["-static"]
}
}
+, "version":
+ { "type": ["@", "rules", "CC", "library"]
+ , "arguments_config": ["SOURCE_DATE_EPOCH"]
+ , "name": ["version"]
+ , "hdrs": ["version.hpp"]
+ , "srcs": ["version.cpp"]
+ , "local defines":
+ { "type": "if"
+ , "cond": {"type": "var", "name": "SOURCE_DATE_EPOCH"}
+ , "then":
+ [ { "type": "join"
+ , "$1":
+ [ "SOURCE_DATE_EPOCH="
+ , { "type": "json_encode"
+ , "$1": {"type": "var", "name": "SOURCE_DATE_EPOCH"}
+ }
+ ]
+ }
+ ]
+ }
+ , "deps": [["@", "json", "", "json"], ["src/utils/cpp", "json"]]
+ , "stage": ["src", "buildtool", "main"]
+ }
}
diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp
index ba8bcb7d..d6d999ab 100644
--- a/src/buildtool/main/main.cpp
+++ b/src/buildtool/main/main.cpp
@@ -29,6 +29,7 @@
#include "src/buildtool/logging/log_config.hpp"
#include "src/buildtool/logging/log_sink_cmdline.hpp"
#include "src/buildtool/logging/log_sink_file.hpp"
+#include "src/buildtool/main/version.hpp"
#include "src/buildtool/multithreading/async_map_consumer.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/utils/cpp/concepts.hpp"
@@ -41,6 +42,7 @@ namespace Target = BuildMaps::Target;
enum class SubCommand {
kUnknown,
+ kVersion,
kDescribe,
kAnalyse,
kBuild,
@@ -134,6 +136,8 @@ auto ParseCommandLineArguments(int argc, char const* const* argv)
CLI::App app("just");
app.option_defaults()->take_last();
+ auto* cmd_version = app.add_subcommand(
+ "version", "Print version information in JSON format.");
auto* cmd_describe = app.add_subcommand(
"describe", "Describe the rule generating a target.");
auto* cmd_analyse =
@@ -152,6 +156,7 @@ auto ParseCommandLineArguments(int argc, char const* const* argv)
app.require_subcommand(1);
CommandLineArguments clargs;
+ SetupDescribeCommandArguments(cmd_version, &clargs);
SetupDescribeCommandArguments(cmd_describe, &clargs);
SetupAnalyseCommandArguments(cmd_analyse, &clargs);
SetupBuildCommandArguments(cmd_build, &clargs);
@@ -169,7 +174,10 @@ auto ParseCommandLineArguments(int argc, char const* const* argv)
std::exit(kExitFailure);
}
- if (*cmd_describe) {
+ if (*cmd_version) {
+ clargs.cmd = SubCommand::kVersion;
+ }
+ else if (*cmd_describe) {
clargs.cmd = SubCommand::kDescribe;
}
else if (*cmd_analyse) {
@@ -1319,6 +1327,11 @@ auto main(int argc, char* argv[]) -> int {
try {
auto arguments = ParseCommandLineArguments(argc, argv);
+ if (arguments.cmd == SubCommand::kVersion) {
+ std::cerr << version() << std::endl;
+ return kExitSuccess;
+ }
+
SetupLogging(arguments.common);
#ifndef BOOTSTRAP_BUILD_TOOL
SetupHashFunction();
diff --git a/src/buildtool/main/version.cpp b/src/buildtool/main/version.cpp
new file mode 100644
index 00000000..3bd37f2a
--- /dev/null
+++ b/src/buildtool/main/version.cpp
@@ -0,0 +1,22 @@
+#include "src/buildtool/main/version.hpp"
+
+#include "nlohmann/json.hpp"
+#include "src/utils/cpp/json.hpp"
+
+auto version() -> std::string {
+ std::size_t major = 0;
+ std::size_t minor = 1;
+ std::size_t revision = 0;
+ std::string suffix = "+devel";
+
+ nlohmann::json version_info = {{"version", {major, minor, revision}},
+ {"suffix", suffix}};
+
+#ifdef SOURCE_DATE_EPOCH
+ version_info["SOURCE_DATE_EPOCH"] = (std::size_t)SOURCE_DATE_EPOCH;
+#else
+ version_info["SOURCE_DATE_EPOCH"] = nullptr;
+#endif
+
+ return IndentOnlyUntilDepth(version_info, 2, 1, {});
+}
diff --git a/src/buildtool/main/version.hpp b/src/buildtool/main/version.hpp
new file mode 100644
index 00000000..c24e87e6
--- /dev/null
+++ b/src/buildtool/main/version.hpp
@@ -0,0 +1,8 @@
+#ifndef INCLUDED_SRC_BUILDOOL_MAIN_VERSION_HPP
+#define INCLUDED_SRC_BUILDOOL_MAIN_VERSION_HPP
+
+#include <string>
+
+auto version() -> std::string;
+
+#endif