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

Configure Crystal compiler version in Makefile #6748

Closed
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ SOURCES := $(shell find src -name '*.cr')
SPEC_SOURCES := $(shell find spec -name '*.cr')
override FLAGS += $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress )$(if $(threads),--threads $(threads) )$(if $(debug),-d )$(if $(static),--static )$(if $(LDFLAGS),--link-flags="$(LDFLAGS)" )
SPEC_FLAGS := $(if $(verbose),-v )$(if $(junit_output),--junit_output $(junit_output) )
EXPORTS := $(if $(release),,CRYSTAL_CONFIG_PATH="$(PWD)/src")
CRYSTAL_CONFIG_VERSION ?= $(shell grep -oEi '(^|\n)\#\s+([0-9]+\.[0-9]+\.[0-9]\w*)' --max-count=1 CHANGELOG.md | awk '{print $$2}')-dev
CRYSTAL_CONFIG_COMMIT ?= $(shell git rev-parse --short HEAD 2> /dev/null)
EXPORTS := $(if $(release),,CRYSTAL_CONFIG_PATH="$(PWD)/src") CRYSTAL_CONFIG_VERSION=$(CRYSTAL_CONFIG_VERSION) CRYSTAL_CONFIG_COMMIT=$(CRYSTAL_CONFIG_COMMIT)
SHELL = sh
LLVM_CONFIG_FINDER := \
[ -n "$(LLVM_CONFIG)" ] && command -v "$(LLVM_CONFIG)" || \
Expand Down Expand Up @@ -60,6 +62,13 @@ else
$(shell echo $(shell printf '\033[33m')Using $(LLVM_CONFIG) [version=$(shell $(LLVM_CONFIG) --version)]$(shell printf '\033[0m') >&2)
endif

ifeq (${CRYSTAL_CONFIG_VERSION},)
$(error Missing version information, could not read from CHANGELOG.md. Please provide a valid CRYSTAL_CONFIG_VERSION setting)
endif
ifeq (${CRYSTAL_CONFIG_VERSION},-dev)
$(error Missing version information, could not read from CHANGELOG.md. Please provide a valid CRYSTAL_CONFIG_VERSION setting)
endif

.PHONY: all
all: crystal ## Build all files (currently crystal only) [default]

Expand Down
50 changes: 15 additions & 35 deletions src/compiler/crystal/config.cr
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
module Crystal
module Config
def self.path
{{env("CRYSTAL_CONFIG_PATH") || ""}}
def self.path : String
{{ env("CRYSTAL_CONFIG_PATH") || "" }}
end

def self.version
version_and_sha.first
def self.version : String
{% begin %}
{% version = env("CRYSTAL_CONFIG_VERSION") || raise "Missing required environment variable CRYSTAL_CONFIG_VERSION" %}
{% compare_versions(version, "0.0.0") %}
{{ version }}
{% end %}
end

def self.build_commit : String?
commit = {{ env("CRYSTAL_CONFIG_COMMIT") }}
return if commit.try &.empty?
commit
end

def self.llvm_version
LibLLVM::VERSION
end

def self.description
version, sha = version_and_sha
sha = build_commit
formatted_sha = "[#{sha}] " if sha
<<-DOC
Crystal #{version} #{formatted_sha}(#{date})
Expand All @@ -23,36 +33,6 @@ module Crystal
DOC
end

@@version_and_sha : {String, String?}?

def self.version_and_sha
@@version_and_sha ||= compute_version_and_sha
end

private def self.compute_version_and_sha
# Set explicitly: 0.0.0, ci, HEAD, whatever
config_version = {{env("CRYSTAL_CONFIG_VERSION")}}
return {config_version, nil} if config_version

git_version = {{`(git describe --tags --long --always 2>/dev/null) || true`.stringify.chomp}}

# Failed git and no explicit version set: ""
# We inherit the version of the compiler building us for now.
return { {{Crystal::VERSION}}, nil } if git_version.empty?

# Shallow clone with no tag in reach: abcd123
# We assume being compiled with the latest released compiler
return {"#{{{Crystal::VERSION}}}+?", git_version} unless git_version.includes? '-'

# On release: 0.0.0-0-gabcd123
# Ahead of last release: 0.0.0-42-gabcd123
tag, commits, sha = git_version.split('-')
sha = sha[1..-1] # Strip g
tag = "#{tag}+#{commits}" unless commits == "0" # Reappend commits since release unless we hit it exactly

{tag, sha}
end

def self.date
{{ `date "+%Y-%m-%d"`.stringify.chomp }}
end
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/crystal/program.cr
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ module Crystal

# Defines a predefined constant in the Crystal module, such as BUILD_DATE and VERSION.
private def define_crystal_constants
version, sha = Crystal::Config.version_and_sha

if sha
if sha = Crystal::Config.build_commit
define_crystal_string_constant "BUILD_COMMIT", sha
else
define_crystal_nil_constant "BUILD_COMMIT"
Expand All @@ -256,7 +254,7 @@ module Crystal
define_crystal_string_constant "DEFAULT_PATH", Crystal::Config.path
define_crystal_string_constant "DESCRIPTION", Crystal::Config.description
define_crystal_string_constant "PATH", Crystal::CrystalPath.default_path
define_crystal_string_constant "VERSION", version
define_crystal_string_constant "VERSION", Crystal::Config.version
define_crystal_string_constant "LLVM_VERSION", Crystal::Config.llvm_version
end

Expand Down