Skip to content
Open
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
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ jobs:
rm -f prog.map
haxelib run hxcpp Build.xml -DWANT_MAP
test -f prog.map
- name: build tool cached libraries of builds with other defines, at the same time too
continue-on-error: ${{ inputs.warn_only }}
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
shell: bash
run: bash test/compile_cache/shared_lib.sh -D$HXCPP_ARCH_FLAG
- name: build tool cached objects and precompiled header that builds make at the same time
continue-on-error: ${{ inputs.warn_only }}
if: ${{ !cancelled() && steps.setup.outcome == 'success' }}
shell: bash
run: bash test/compile_cache/same_objects.sh -D$HXCPP_ARCH_FLAG
- name: build tool fails again after a step after the link failed
continue-on-error: ${{ inputs.warn_only }}
if: ${{ !cancelled() && steps.setup.outcome == 'success' && runner.os == 'Windows' }}
Expand Down
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ libhxcpp 6.0.3 (unreleased)
------------------------------------------------------------

* Fixed the C++ build tool, built with haxe compile-cpp.hxml in tools/hxcpp, not compiling since 6.0.0: ThreadPool.hx declared the class that ends a compile thread's job before one of its imports, which Haxe refuses with "import and using may not appear after a declaration". The neko build tool, hxcpp.n, was unaffected, and run.n uses a C++ build tool only once one is built, so only rebuilding it failed. CI now type-checks it (libhxcpp #471)
* Fixed builds that share one HXCPP_COMPILE_CACHE with other defines, as a cppia host with HXCPP_DEBUGGER and one without, linking each other's libraries when they ran at the same time: a cached group linked as a library, as the runtime, hxcpp-cppia and hxcpp_sqlite are, went to one file for each toolchain and project, so a build could link the other's, and fail on its debugger symbols or crash, link one half written, or fail to open it (LNK1104). One after the other, each build linked it again. The library's name now comes from its objects and how it is linked, and it is linked under a name of the build's own and then moved into place, so each build links its own and builds with the same objects share one. Libraries under the old names are no longer used, and trimming the cache removes them in time. Stock hxcpp names the library the same way
* Fixed builds that share one HXCPP_COMPILE_CACHE with the same defines, as two projects built at once, failing when they ran at the same time from a cache without their objects: each compiler wrote a cached object or precompiled header in place, so one build failed to write it while another did, as hxcpp.h's did with "Could not create PCH" (C1083), or compiled or linked with one half written, or one another build deleted after its own write failed. Two builds making the cache's own directories at once also failed with "Could not create compiler cache directory". Each object and precompiled header is now made under a name of the build's own and moved into place whole, the header with its object in one directory, and when two builds make the same one, the first in place is kept. Stock hxcpp writes them in place too
* Fixed a cppia cast to a class or interface whose value goes unused, cast(o, SomeInterface) as a statement, checking nothing under the JIT: a cast that fails ran on, where the interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes such casts in closures, which the JIT now compiles. The JIT leaves out only the unused casts that cannot fail
* Fixed the collector marking and moving the variables a cppia closure captures in place in the closure: a closure packs its captures at 4-byte offsets, so on 64-bit a String or object captured after an Int, as text in var count = 1; var text = "x"; function() return count + text, sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. Each capture is copied out to be marked or visited now, and written back if the moving collector moved it. Interpreted and jitted closures were both affected
* Fixed the cppia interpreter reading an argument's slot in place in the script frame when it fills in the argument's default value, and writing a Float or Dynamic default the same way: a frame keeps 4-byte alignment, so on 64-bit an argument after an Int, as b in f(a:Int, ?b:String = "x"), sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. The slots are copied in and out now, as the interpreter's other frame accesses have been since 6.0.0. The JIT, which compiles defaults into the function, was not affected
Expand Down
4 changes: 4 additions & 0 deletions docs/CompileCache.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The cache uses a hashing mechanism to tell if the contents of a file or its depe

Additional benefits include keeping some files outside the source tree, and being able to remove these temp files easily.

Builds can use one cache at the same time, as two projects built at once do. Each object, precompiled header and library is made under a name of the build's own and then moved into place whole, so no build reads one that another build is still writing, and when two builds make the same one, the first in place is kept.

### Setup
A directory needs to be set aside to enable the cache. If possible, this should be on fast storage, such as a SSD. This is most easily done with an entry in the .hxcpp_config.xml file:
```xml
Expand Down Expand Up @@ -33,6 +35,8 @@ When linking a file group 'asLibrary', the object files are compiled and then th
then the constructor may not get called, leading to bugs.
- Can help on some systems where the linker command-line length is an issue.

The library goes in the cache, named by the objects in it and how it is linked. Builds with other defines, which compile other objects, each link a library of their own, so they can share a cache at the same time, and builds with the same objects use one library.


### Management
When compiling normally, hxcpp will check the cache size and evict the least used files to maintain the specified cache size.
Expand Down
71 changes: 71 additions & 0 deletions test/compile_cache/same_objects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
# Builds that share one HXCPP_COMPILE_CACHE with the same defines, as two
# projects built at once do, compile the same cached objects and make the
# same cached precompiled header, as the runtime's and hxcpp.h's are. Each
# compiler wrote them in place, so from an empty cache one build failed to
# write them while another did (C1083), or compiled or linked with one half
# written, or deleted, after its own write failed (C1083, LNK1104).
# This builds same_objects/Build.xml, whose cached group with a precompiled
# header has its sources in one folder, in four folders of its own at once,
# from an empty cache, three times. Each build must print 36, and leave
# nothing half made in the cache.
# Usage: test/compile_cache/same_objects.sh [extra build tool args, eg -DHXCPP_M64]
cd "$(dirname "$0")"
extra=("$@")
rm -rf bin/same_objects
mkdir -p bin/same_objects/shared
cp same_objects/pre.h same_objects/main.cpp bin/same_objects/shared/
cd bin/same_objects
for part in 0 1 2 3 4 5 6 7; do
cat > "shared/part$part.cpp" <<EOF
#include <pre.h>

int part$part()
{
std::map<std::string, int> seen;
for (int i = 0; i <= $part; i++)
seen[std::to_string(i)] = i;
return (int)seen.size();
}
EOF
done
# A path the compiler reads as it is, on Windows too
shared=$(cd shared && { pwd -W 2>/dev/null || pwd; })
failed=0

# build <folder>: builds in a folder of its own and checks what it prints
build() {
mkdir "$1"
cp ../../same_objects/Build.xml "$1/"
if ! (cd "$1" && haxelib run hxcpp Build.xml -DSHARED="$shared" "${extra[@]}" > build.log 2>&1); then
echo "::error::the build in $1 failed"
tail -20 "$1/build.log"
return 1
fi
local printed
printed=$("$1/prog" | tr -d '\r')
if [ "$printed" != 36 ]; then
echo "::error::the program built in $1 printed $printed"
return 1
fi
echo "the build in $1 printed 36"
}

for round in 1 2 3; do
export HXCPP_COMPILE_CACHE="$PWD/cache$round"
pids=()
for folder in a b c d; do
build "$folder$round" > "$folder$round.out" 2>&1 &
pids+=($!)
done
for pid in "${pids[@]}"; do
wait $pid || failed=1
done
cat a$round.out b$round.out c$round.out d$round.out
left=$(find "$HXCPP_COMPILE_CACHE" -name '*.tmp-*' -o -name '*.old-*')
if [ -n "$left" ]; then
echo "::error::the builds left entries half made in the cache: $left"
failed=1
fi
done
exit $failed
24 changes: 24 additions & 0 deletions test/compile_cache/same_objects/Build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<xml>
<!-- A cached group with a precompiled header, its sources in a folder
every build shares, as the runtime's are in HXCPP: builds in folders
of their own compile the same objects into the cache, and make the
same precompiled header there -->
<files id="cached" dir="${SHARED}">
<compilerflag value="-I${SHARED}" />
<precompiledheader name="pre" dir="${SHARED}" />
<cache value="1" project="same_objects_test" />
<file name="main.cpp" />
<file name="part0.cpp" />
<file name="part1.cpp" />
<file name="part2.cpp" />
<file name="part3.cpp" />
<file name="part4.cpp" />
<file name="part5.cpp" />
<file name="part6.cpp" />
<file name="part7.cpp" />
</files>

<target id="default" tool="linker" toolid="exe" output="prog">
<files id="cached" />
</target>
</xml>
16 changes: 16 additions & 0 deletions test/compile_cache/same_objects/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <pre.h>

int part0();
int part1();
int part2();
int part3();
int part4();
int part5();
int part6();
int part7();

int main()
{
printf("%d\n", part0() + part1() + part2() + part3() + part4() + part5() + part6() + part7());
return 0;
}
7 changes: 7 additions & 0 deletions test/compile_cache/same_objects/pre.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <map>
#include <sstream>
#include <string>
#include <vector>
112 changes: 112 additions & 0 deletions test/compile_cache/shared_lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env bash
# Builds that share one HXCPP_COMPILE_CACHE with other defines, as a host
# with HXCPP_DEBUGGER and one without do, compile other objects for a cached
# group linked as a library, as the runtime is. The library was named by the
# toolchain and the project alone, so they linked their objects into the same
# file: one after the other, each linked it again, and at the same time one
# could link the other's library, as unresolved debugger symbols showed, or
# one half written, or fail to open it (LNK1104).
# This builds shared_lib/Build.xml, whose library returns VALUE, in folders
# of its own with one cache:
# - One after the other, a build with another VALUE must leave the first
# one's library as it was, and the first build again must link nothing.
# With its object trimmed from the cache, and so compiled again under the
# same name, it must link its library again in its place.
# - With the libraries gone from the cache but their objects kept, four builds
# at once, two with VALUEs of their own and two with the same one, must each
# link and print their own VALUE, and leave no half made library behind.
# Usage: test/compile_cache/shared_lib.sh [extra build tool args, eg -DHXCPP_M64]
cd "$(dirname "$0")"
extra=("$@")
rm -rf bin/shared_lib
mkdir -p bin/shared_lib
cd bin/shared_lib
export HXCPP_COMPILE_CACHE="$PWD/cache"
libs=cache/shared_lib_test/lib
failed=0

# build <folder> <value>: builds in a folder of its own and checks that the
# program prints the value. The sources keep their dates, as a newer source
# compiles its cached object again
build() {
if [ ! -d "$1" ]; then
mkdir "$1"
cp -p ../../shared_lib/* "$1/"
fi
if ! (cd "$1" && haxelib run hxcpp Build.xml -DVALUE="$2" "${extra[@]}" > build.log 2>&1); then
echo "::error::the build in $1 with VALUE=$2 failed"
tail -20 "$1/build.log"
return 1
fi
local printed
printed=$("$1/prog" | tr -d '\r')
if [ "$printed" != "$2" ]; then
echo "::error::the program built in $1 with VALUE=$2 printed $printed"
return 1
fi
echo "the build in $1 printed $2"
}

# The libraries in the cache, each with a checksum
list_libs() {
find "$libs" -type f ! -name '*.hash' -exec cksum {} + | sort -k3
}

build one 1 || exit 1
first=$(list_libs)
first_objects=$(find cache/shared_lib_test -path "$libs" -prune -o -type f -print)
build two 2 || exit 1
after=$(list_libs)
if [ -n "$first" ] && grep -qxF "$first" <<< "$after"; then
echo "the build with another VALUE left the first one's library as it was"
else
echo "::error::the build with another VALUE changed the library the first one linked"
echo "before: $first"
echo "after: $after"
failed=1
fi
build one 1 || failed=1
if grep -q "Link:.*shared_lib_test" one/build.log; then
echo "::error::the first build linked its library again:"
grep "Link:" one/build.log
failed=1
else
echo "the first build again linked no library"
fi
# An object trimmed from the cache is compiled again under the same name.
# The build tool reads dates to the second, and links again only for an
# object newer than the library, which a fast build can compile in the
# same second as the library was linked
sleep 1
rm -f $first_objects
build one 1 || failed=1
if grep -q "Link:.*shared_lib_test" one/build.log && [ "$(list_libs | wc -l)" -eq 2 ]; then
echo "an object compiled again linked its library again in its place"
else
echo "::error::an object compiled again did not link its library again in its place:"
grep "Link:" one/build.log
list_libs
failed=1
fi

for round in 1 2 3; do
same=$((round * 10 + 3))
for value in $((round * 10 + 1)) $((round * 10 + 2)) $same; do
build "objects$value" $value > /dev/null || exit 1
done
rm -f "$libs"/*
build "a$round" $((round * 10 + 1)) > "a$round.out" 2>&1 & a=$!
build "b$round" $((round * 10 + 2)) > "b$round.out" 2>&1 & b=$!
build "c$round" $same > "c$round.out" 2>&1 & c=$!
build "d$round" $same > "d$round.out" 2>&1 & d=$!
for pid in $a $b $c $d; do
wait $pid || failed=1
done
cat "a$round.out" "b$round.out" "c$round.out" "d$round.out"
done
left=$(find "$libs" -name '*.tmp-*')
if [ -n "$left" ]; then
echo "::error::the builds left libraries half made: $left"
failed=1
fi
exit $failed
20 changes: 20 additions & 0 deletions test/compile_cache/shared_lib/Build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<xml>
<!-- A cached group linked as a library, as the runtime is, whose object
changes with VALUE, as the runtime's do with HXCPP_DEBUGGER. It comes
first, so each build compiles main.cpp between linking the library and
linking the program -->
<files id="lib">
<cache value="1" project="shared_lib_test" asLibrary="true" />
<compilerflag value="-DVALUE=${VALUE}" />
<file name="lib.cpp" />
</files>

<files id="main">
<file name="main.cpp" />
</files>

<target id="default" tool="linker" toolid="exe" output="prog">
<files id="lib" />
<files id="main" />
</target>
</xml>
1 change: 1 addition & 0 deletions test/compile_cache/shared_lib/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int libValue() { return VALUE; }
9 changes: 9 additions & 0 deletions test/compile_cache/shared_lib/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

int libValue();

int main()
{
printf("%d\n", libValue());
return 0;
}
4 changes: 4 additions & 0 deletions tools/hxcpp/BuildTool.hx
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,10 @@ class BuildTool
var libName = targetDir + "/" + mCompiler.getTargetPrefix() + "_" + group.getCacheProject();

var libTarget = new Target(libName, "linker", "static_link" );
// Another build with other defines can link the same group into
// the cache at the same time: the linker names this one by what
// goes in it, so each build links its own
libTarget.mInCache = useCache;
linker.link(libTarget,groupObjs, mCompiler, [] );
target.mAutoLibs.push(linker.mLastOutName);
// Linux the libraries must be added again if the references were not resolved the firs time
Expand Down
Loading
Loading