Skip to content
Merged
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
13 changes: 9 additions & 4 deletions dev/lowir_library.mk
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Independently built PIC objects keep library flags out of normal tool and
# self-host objects. Public C symbols are the only exports: the link names
# Independently built PIC objects keep library flags out of normal tool builds.
# A caller may reuse shared objects built with the same compiler and library
# flags, after updating them through their owning build (the release does this).
# Public C symbols are the only exports: the link names
# them, so the exports are the same when cppgm compiles the objects (it accepts
# the visibility options and does not apply them). The host compiler links the
# shared library, under its own name: a client finds it on its run path.
LOWIR_LIBRARY_OBJDIR = $(OBJ)/lowir-library
LOWIR_LIBRARY_TOOLCHAIN_STAMP = .lowir-library-toolchain
LOWIR_LIBRARY_FLAGS = $(CC_FLAGS) -pthread -fPIC -fvisibility=hidden -fvisibility-inlines-hidden
LOWIR_LIBRARY_OBJS = $(addprefix $(LOWIR_LIBRARY_OBJDIR)/,$(addsuffix .o,$(FRONTEND_LIBRARY_SOURCE_IDS_lowir_library))) $(LOWIR_LIBRARY_OBJDIR)/boundary.o
LOWIR_LIBRARY_REUSE_OBJDIR ?=
LOWIR_LIBRARY_REUSE_SOURCES = $(if $(LOWIR_LIBRARY_REUSE_OBJDIR),$(filter $(all_obj_basenames),$(FRONTEND_LIBRARY_SOURCE_IDS_lowir_library)))
lowir_library_object = $(if $(filter $(1),$(LOWIR_LIBRARY_REUSE_SOURCES)),$(LOWIR_LIBRARY_REUSE_OBJDIR),$(LOWIR_LIBRARY_OBJDIR))/$(1).o
LOWIR_LIBRARY_OBJS = $(foreach source,$(FRONTEND_LIBRARY_SOURCE_IDS_lowir_library),$(call lowir_library_object,$(source))) $(LOWIR_LIBRARY_OBJDIR)/boundary.o
LOWIR_LIBRARY_SHARED = libcppgm_lowir.$(if $(filter Darwin,$(shell uname -s)),dylib,so)
LOWIR_LIBRARY_EXPORTS = $(LOWIR_LIBRARY_OBJDIR)/exports
# CXX is the host compiler when no other is named.
Expand Down Expand Up @@ -35,7 +40,7 @@ $(LOWIR_LIBRARY_EXPORTS): $(LOWIR_LIBRARY_OBJDIR)/.config
# Libraries share output paths across isolated object roots. Remember the
# selected root as well as flags so returning to an older cell relinks it.
$(LOWIR_LIBRARY_TOOLCHAIN_STAMP): FORCE
@printf '%s\n%s\n%s\n%s\n' '$(CXX)' '$(LOWIR_LIBRARY_LINKER)' '$(LOWIR_LIBRARY_FLAGS)' '$(abspath $(LOWIR_LIBRARY_OBJDIR))' > $@.tmp
@printf '%s\n%s\n%s\n%s\n%s\n' '$(CXX)' '$(LOWIR_LIBRARY_LINKER)' '$(LOWIR_LIBRARY_FLAGS)' '$(abspath $(LOWIR_LIBRARY_OBJDIR))' '$(LOWIR_LIBRARY_REUSE_OBJDIR)' > $@.tmp
@if ! cmp -s $@.tmp $@ 2>/dev/null; then mv -f $@.tmp $@; else rm -f $@.tmp; fi

$(LOWIR_LIBRARY_OBJDIR)/.config: FORCE
Expand Down
9 changes: 6 additions & 3 deletions scripts/build_release_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ host=${1:?usage: build_release_tools.sh <host compiler> [make argument ...]}
shift
root=$(cd "$(dirname "$0")/.." && pwd)
stage=obj/release
library_flags='-pthread -fPIC -fvisibility=hidden -fvisibility-inlines-hidden'

make -C "$root/dev" all CXX="$host" CPPGM_HOST_CXX="$host" "$@"
make -C "$root/pa35" compare-inception CXX=../dev/cppgm CPPGM_HOST_CXX="$host" \
INCEPTION_OBJ_ROOT_BASE="../$stage" "$@"
# A library object does not name its compiler as a prerequisite: compile each one again.
INCEPTION_OBJ_ROOT_BASE="../$stage/pic" INCEPTION_BIN_ROOT_BASE="../$stage/bin" \
INCEPTION_EXTRA_CC_FLAGS="$library_flags ${INCEPTION_EXTRA_CC_FLAGS:-}" "$@"
# Third-stage shared objects were compiled by cppgm-self with library flags.
# Only the library-specific objects need rebuilding when that compiler changes.
make -C "$root/dev" clean-lowir-library OBJ="../$stage/library"
make -C "$root/dev" lowir-library CXX="$root/$stage/bin/selfhost/cppgm-self" CPPGM_HOST_CXX="$host" \
OBJ="../$stage/library" "$@"
OBJ="../$stage/library" LOWIR_LIBRARY_REUSE_OBJDIR="../$stage/pic/inception/shared/release" "$@"
73 changes: 73 additions & 0 deletions scripts/tests/test_package_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"""A release package holds the self-compiled tools, the LowIR library and its headers, with fixed bytes."""
import json
from pathlib import Path
import shutil
import subprocess
import sys
import tarfile
import tempfile
import time
import unittest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
Expand Down Expand Up @@ -39,6 +41,77 @@ def tree(root):


class PackageRelease(unittest.TestCase):
def test_library_reuses_objects_and_relinks_when_their_root_changes(self):
host = shutil.which('c++')
make = shutil.which('make')
if not host or not make:
self.skipTest('a host C++ compiler and make are required')
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / 'src').mkdir()
(root / 'src/core.cpp').write_text('int core() { return 7; }\n')
(root / 'src/api.cpp').write_text(
'int core();\nextern "C" __attribute__((visibility("default"))) '
'int cppgm_lowir_value() { return core(); }\n')
(root / 'lowir_library.cpp').write_text('int library_boundary;\n')
shutil.copyfile(ROOT / 'dev/lowir_library.mk', root / 'lowir_library.mk')
(root / 'Makefile').write_text(
'SRC=src\nOBJ=obj\nCC_FLAGS=-O0\nINC=-Isrc\n'
'FRONTEND_LIBRARY_SOURCE_IDS_lowir_library=core api\n'
'all_obj_basenames=core\nquiet=\nQ=@\n.PHONY: FORCE\nFORCE:\n'
'include lowir_library.mk\n')
compiler = root / 'compiler'
compiler.write_text('#!' + sys.executable + '\nimport json, os, sys\n'
'with open(' + repr(str(root / 'commands')) + ', "a") as out:\n'
' out.write(json.dumps(sys.argv[1:]) + "\\n")\n'
'os.execv(' + repr(host) + ', [' + repr(host) + '] + sys.argv[1:])\n')
compiler.chmod(0o755)
for name, value in [('third-stage', 7), ('other-stage', 9)]:
(root / name).mkdir()
subprocess.run([host, '-fPIC', '-x', 'c++', '-c', '-o', str(root / name / 'core.o'), '-'],
input='int core() { return %s; }' % value, text=True, check=True)

def build(reuse):
return subprocess.run([make, 'lowir-library', 'CXX=' + str(compiler),
'CPPGM_HOST_CXX=' + host, 'LOWIR_LIBRARY_REUSE_OBJDIR=' + reuse],
cwd=root, capture_output=True, text=True)

def compiled():
return [args for args in map(json.loads, (root / 'commands').read_text().splitlines())
if '-c' in args]

(root / 'client.cpp').write_text(
'extern "C" int cppgm_lowir_value();\nint main() { return cppgm_lowir_value(); }\n')

def result(library):
subprocess.run([host, str(root / 'client.cpp'), str(root / library), '-o', str(root / 'client')],
check=True)
return subprocess.run([str(root / 'client')]).returncode

first = build('third-stage')
self.assertEqual(first.returncode, 0, first.stdout + first.stderr)
self.assertEqual(len(compiled()), 2) # API and boundary, with no duplicate core compile.
shared = 'libcppgm_lowir.dylib' if sys.platform == 'darwin' else 'libcppgm_lowir.so'
# A shared client locates the library's install name / soname at runtime.
shared_result = subprocess.check_output(
[sys.executable, '-c', 'import ctypes, sys; print(ctypes.CDLL(sys.argv[1]).cppgm_lowir_value())',
str(root / shared)], text=True).strip()
self.assertEqual(shared_result, '7')
self.assertEqual(result('libcppgm_lowir.a'), 7)
self.assertEqual(build('third-stage').returncode, 0)
self.assertEqual(len(compiled()), 2)
# Both roots predate the libraries: the toolchain stamp must force a relink.
time.sleep(1.1) # Apple make 3.81 compares mtimes at whole-second precision.
self.assertEqual(build('other-stage').returncode, 0)
self.assertEqual(result('libcppgm_lowir.a'), 9)
self.assertEqual(len(compiled()), 2)
(root / 'other-stage/core.o').unlink()
self.assertNotEqual(build('other-stage').returncode, 0)
time.sleep(1.1)
self.assertEqual(build('').returncode, 0) # Ordinary builds still compile their own core.
self.assertEqual(len(compiled()), 3)
self.assertEqual(result('libcppgm_lowir.a'), 7)

def test_package_is_reproducible_and_complete(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
Expand Down
Loading