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

[WIP] Explicitly specify target for Darwin Intel crystal build #265

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 9 additions & 9 deletions darwin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CRYSTAL_SHA1 ?= $(CRYSTAL_VERSION) ## Git tag/branch/sha1 to checkout and build
PACKAGE_ITERATION ?= 1
FORCE_GIT_TAGGED ?= 1 ## Require build to be based on git tag/branch

PREVIOUS_CRYSTAL_RELEASE_DARWIN_TARGZ ?= ## url to crystal-{version}-{package}-darwin-x86_64.tar.gz
PREVIOUS_CRYSTAL_RELEASE_DARWIN_TARGZ ?= ## url to crystal-{version}-{package}-darwin-universal.tar.gz. Eg: https://github.com/crystal-lang/crystal/releases/download/1.10.1/crystal-1.10.1-1-darwin-universal.tar.gz

OUTPUT_DIR = build

Expand Down Expand Up @@ -40,15 +40,15 @@ help: ## Show this help
awk 'BEGIN {FS = "## "}; /^## [a-zA-Z_-]/ {printf " \033[36m%s\033[0m\n", $$2}; /^## / {printf " %s\n", $$2}'

.PHONY: darwin-previous
darwin-previous: $(CURDIR)/../omnibus/crystal-darwin-x86_64/embedded/bin/crystal ## download previous crystal darwin release
darwin-previous: $(CURDIR)/../omnibus/crystal-darwin-universal/embedded/bin/crystal ## download previous crystal darwin release

# Once there are prior builds for arm64, this can use DARWIN_ARCH
$(CURDIR)/../omnibus/crystal-darwin-x86_64/embedded/bin/crystal:
curl -L -o /tmp/crystal-darwin-x86_64.tar.gz $(PREVIOUS_CRYSTAL_RELEASE_DARWIN_TARGZ) \
&& mkdir -p $(CURDIR)/../omnibus/crystal-darwin-x86_64 \
&& tar xfz /tmp/crystal-darwin-x86_64.tar.gz -C $(CURDIR)/../omnibus/crystal-darwin-x86_64 --strip-components=1 \
&& rm /tmp/crystal-darwin-x86_64.tar.gz \
&& chmod +x $(CURDIR)/../omnibus/crystal-darwin-x86_64/embedded/bin/crystal
$(CURDIR)/../omnibus/crystal-darwin-universal/embedded/bin/crystal:
curl -L -o /tmp/crystal-darwin-universal.tar.gz $(PREVIOUS_CRYSTAL_RELEASE_DARWIN_TARGZ) \
&& mkdir -p $(CURDIR)/../omnibus/crystal-darwin-universal \
&& tar xfz /tmp/crystal-darwin-universal.tar.gz -C $(CURDIR)/../omnibus/crystal-darwin-universal --strip-components=1 \
&& rm /tmp/crystal-darwin-universal.tar.gz \
&& chmod +x $(CURDIR)/../omnibus/crystal-darwin-universal/embedded/bin/crystal

$(OUTPUT_DIR)/$(DARWIN_NAME) $(OUTPUT_DIR)/$(DARWIN_PKG_NAME): ## Build omnibus crystal project
ifeq ($(FORCE_GIT_TAGGED), 0)
Expand All @@ -75,6 +75,6 @@ clean: ## Clean up build directory
rm -Rf $(CURDIR)/tmp
rm -Rf $(CURDIR)/../omnibus/pkg/crystal-*
rm -Rf $(CURDIR)/../omnibus/pkg/version-*
rm -Rf $(CURDIR)/../omnibus/crystal-darwin-*
rm -Rf $(CURDIR)/../omnibus/crystal-darwin-universal
rm -Rf /var/cache/omnibus/*
rm -Rf /opt/crystal/*
82 changes: 61 additions & 21 deletions omnibus/config/software/crystal.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
CRYSTAL_VERSION = ENV['CRYSTAL_VERSION']
CRYSTAL_SHA1 = ENV['CRYSTAL_SHA1']
FIRST_RUN = ENV["FIRST_RUN"]
CRYSTAL_SRC = (ENV['CRYSTAL_SRC'] || "").strip
CRYSTAL_SRC = ENV.fetch('CRYSTAL_SRC', 'https://github.com/crystal-lang/crystal')

name "crystal"
default_version CRYSTAL_VERSION
skip_transitive_dependency_licensing true

if CRYSTAL_SRC.empty?
source git: "https://github.com/crystal-lang/crystal"
else
source git: CRYSTAL_SRC
end
source git: CRYSTAL_SRC

dependency "pcre2"
dependency "bdw-gc"
Expand Down Expand Up @@ -46,40 +42,84 @@
end

build do
command "git checkout #{CRYSTAL_SHA1}", cwd: project_dir
block { puts "\n=== Starting a build phase for crystal ===\n\n" }

command "git checkout '#{CRYSTAL_SHA1}'", cwd: project_dir

# Native crystal binary with cross-platform functionality
block { puts "\n=== Build crystal's deps in #{project_dir}\n\n" }

mkdir "#{project_dir}/deps"
make "deps", env: env
make "deps", env: env.dup
mkdir ".build"
command "echo #{Dir.pwd}", env: env

block { puts "\n=== Build native crystal bin with embedded universal crystal binary\n\n" }
copy "#{Dir.pwd}/crystal-darwin-universal/embedded/bin/crystal", ".build/crystal"
command ".build/crystal --version", env: env.dup
command "file .build/crystal", env: env.dup

crflags = "--no-debug"

copy "#{Dir.pwd}/crystal-#{ohai['os']}-#{ohai['kernel']['machine']}/embedded/bin/crystal", ".build/crystal"

# Compile for Intel
command "make crystal stats=true release=true FLAGS=\"#{crflags}\" CRYSTAL_CONFIG_LIBRARY_PATH= O=#{output_path}", env: env
move output_bin, "#{output_bin}_x86_64"
command "make crystal stats=true release=true FLAGS=\"#{crflags}\" CRYSTAL_CONFIG_LIBRARY_PATH= O=#{output_path}", env: env.dup
block "Testing the result file" do
puts "===> Testing the result file #{output_bin}"
raise "Could not build native crystal binary: #{output_bin}" unless File.exist?("#{output_bin}")
end
command "file #{output_bin}", env: env.dup
command "#{output_bin} --version", env: env.dup

# Restore compiler w/ cross-compile support
block { puts "\n\n=== Restore compiler with cross-compile support ===\n\n" }
move "#{output_bin}", ".build/crystal"

make "clean_cache clean", env: env

# x86_64 crystal binary
block { puts "\n\n=== Building x86_64 binary ===\n\n" }

original_CXXFLAGS_env = env["CXXFLAGS"].dup
original_LDFLAGS_env = env["LDFLAGS"].dup

env["CXXFLAGS"] = original_CXXFLAGS_env + " -target x86_64-apple-darwin"
env["LDFLAGS"] = original_LDFLAGS_env + " -v -target x86_64-apple-darwin"
env["LDLIBS"] = "-v -target x86_64-apple-darwin"
make "deps", env: env.dup

make "crystal verbose=true stats=true release=true target=x86_64-apple-darwin CRYSTAL_CONFIG_TARGET=x86_64-apple-darwin FLAGS=\"#{crflags}\" CRYSTAL_CONFIG_LIBRARY_PATH= O=#{output_path}", env: env
command "clang #{output_path}/crystal.o -o #{output_bin}_x86_64 -target x86_64-apple-darwin src/llvm/ext/llvm_ext.o `llvm-config --libs --system-libs --ldflags 2>/dev/null` -lstdc++ -lpcre2-8 -lgc -lpthread -levent -liconv -ldl -v", env: env
block "Testing the result file" do
puts "===> Testing the result file #{output_bin}_x86_64"
raise "Could not build crystal x86_64" unless File.exist?("#{output_bin}_x86_64")
end
# NOTE: Add validation of the output
command "file #{output_bin}_x86_64", env: env

# Clean up
make "clean_cache clean", env: env

# Restore x86_64 compiler w/ cross-compile support
mkdir ".build"
copy "#{output_bin}_x86_64", ".build/crystal"

# arm64 crystal binary
block { puts "\n\n=== Building arm64 version ===\n\n" }

# Compile for ARM64. Apple's clang only understands arm64, LLVM uses aarch64,
# so we need to sub out aarch64 in our calls to Apple tools
env["CXXFLAGS"] << " -target arm64-apple-darwin"
env["CXXFLAGS"] = original_CXXFLAGS_env + " -target arm64-apple-darwin"
make "deps", env: env

make "crystal stats=true release=true target=aarch64-apple-darwin FLAGS=\"#{crflags}\" CRYSTAL_CONFIG_TARGET=aarch64-apple-darwin CRYSTAL_CONFIG_LIBRARY_PATH= O=#{output_path}", env: env

command "clang #{output_path}/crystal.o -o #{output_bin}_arm64 -target arm64-apple-darwin src/llvm/ext/llvm_ext.o `llvm-config --libs --system-libs --ldflags 2>/dev/null` -lstdc++ -lpcre2-8 -lgc -lpthread -levent -liconv -ldl -v", env: env

block "Testing the result file" do
puts "===> Testing the result file #{output_bin}_arm64"
raise "Could not build crystal arm64" unless File.exist?("#{output_bin}_arm64")
end
# NOTE: Add validation of the output
command "file #{output_bin}_arm64", env: env
delete "#{output_path}/crystal.o"

# Lipo them up
block { puts "\n\n=== Combine x86_64 and arm64 binaries in single universal binary ===\n\n" }
command "lipo -create -output #{output_bin} #{output_bin}_x86_64 #{output_bin}_arm64"

# Clean up
delete "#{output_bin}_x86_64"
delete "#{output_bin}_arm64"

Expand Down