Skip to content

Commit 5717819

Browse files
committed
Merge branch 'test_on_mac'
2 parents aff5316 + ece912d commit 5717819

File tree

5 files changed

+107
-18
lines changed

5 files changed

+107
-18
lines changed

.github/workflows/dockcross.yml

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,47 @@ jobs:
4545
tag_name: ${{ github.ref_name == 'master' && steps.version.outputs.version || format ('{0}-{1}', steps.version.outputs.version, steps.vars.outputs.COMMIT_SHORT_SHA) }}
4646

4747

48+
build_on_mac:
49+
needs: create_release
50+
strategy:
51+
fail-fast: false
52+
matrix:
53+
os: [macos-latest, macos-13]
54+
55+
runs-on: ${{ matrix.os }}
56+
57+
steps:
58+
- uses: actions/checkout@v4
59+
60+
- name: Install Dependencies
61+
run: brew install libffi readline llvm
62+
63+
- name: Configure CMake
64+
run: brew sh -c "mkdir build && cd build && cmake .."
65+
66+
- name: Build
67+
run: brew sh -c "cd build && make -j 4"
68+
69+
- name: Run Tests
70+
run: cd build && ctest --output-on-failure --parallel || ctest --rerun-failed --output-on-failure --extra-verbose
71+
72+
- name: rename_executable
73+
run: |
74+
mv build/cliffi cliffi-mac-${{ (matrix.os == 'macos-latest') && 'arm64' || 'x64' }}
75+
76+
- name: upload
77+
uses: softprops/action-gh-release@v2
78+
with:
79+
tag_name: ${{ needs.create_release.outputs.tag_name }}
80+
files: cliffi-mac-${{ (matrix.os == 'macos-latest') && 'arm64' || 'x64' }}
81+
draft: true
82+
prerelease: true
83+
84+
- name: Setup tmate session if failed
85+
if: ${{ failure() }}
86+
uses: mxschmitt/action-tmate@v3
87+
88+
4889
build:
4990
runs-on: ubuntu-latest
5091
needs: create_release
@@ -238,18 +279,18 @@ jobs:
238279

239280

240281
smoke_test:
241-
needs: [ build, create_release ]
282+
needs: [ build, build_on_mac, create_release ]
242283
strategy:
243284
fail-fast: false
244285
matrix:
245-
os: [ubuntu-latest, windows-latest]
286+
os: [ubuntu-latest, windows-latest,macos-latest]
246287
runs-on: ${{ matrix.os }}
247288
steps:
248289
- name: Download Release
249290
uses: robinraju/release-downloader@v1.12
250291
with:
251292
releaseId: ${{ needs.create_release.outputs.release_id }}
252-
fileName: cliffi-${{ matrix.os == 'windows-latest' && 'windows-static-x64.exe' || 'linux-x64' }}
293+
fileName: cliffi-${{ matrix.os == 'windows-latest' && 'windows-static-x64.exe' || matrix.os == 'ubuntu-latest' && 'linux-x64' || 'mac-arm64' }}
253294

254295
- name: Run Smoke Test (Windows)
255296
if: runner.os == 'Windows'
@@ -268,6 +309,15 @@ jobs:
268309
./cliffi libc i strlen test 2>&1 | tee output
269310
grep -q "Function returned: 4" output || exit 1
270311
312+
- name: Run Smoke Test (Mac)
313+
if: runner.os == 'macOS'
314+
run: |
315+
ls -l
316+
mv cliffi-* cliffi
317+
chmod +x cliffi
318+
./cliffi libSystem i strlen test 2>&1 | tee output
319+
grep -q "Function returned: 4" output || exit 1
320+
271321
- name: Setup tmate session if failed
272322
if: ${{ failure() }}
273323
uses: mxschmitt/action-tmate@v3

dockcross_buildtest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ conan install . --output-folder=build --build=missing -pr build -g=CMakeDeps
88
cd build
99
cmake .. -DUSE_FIND_PACKAGE=ON -DCMAKE_C_FLAGS="-Werror"
1010
make
11-
ctest --output-on-failure || ctest --rerun-failed --output-on-failure --extra-verbose
11+
ctest --output-on-failure --parallel || ctest --rerun-failed --output-on-failure --extra-verbose

exception_handling.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <signal.h>
1818
#include <stdint.h>
1919
/*x86 or risc based systems*/
20-
#if (defined(__i386__) || defined(__x86_64__) || defined(__riscv)) && !defined(_WIN32)
20+
#if (defined(__i386__) || defined(__x86_64__) || defined(__riscv)) && !defined(_WIN32) && !defined(__APPLE__)
2121
#include <ucontext.h>
2222
#endif
2323
#include "shims.h"

library_path_resolver.c

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static bool FindInStandardPaths(const char* library_name, char* resolved_path) {
169169
#if defined(__ANDROID__)
170170
const char* standard_paths[] = {"/system/lib64", "/system/lib", "/system/vendor/lib64", "/system/vendor/lib", NULL};
171171
#elif defined(__APPLE__)
172-
const char* standard_paths[] = {"/usr/lib", "/lib", "/usr/local/lib", "/opt/local/lib", "/opt/homebrew/lib", NULL};
172+
const char* standard_paths[] = {"/usr/lib", "/usr/lib/system", "/lib", "/usr/local/lib", "/opt/local/lib", "/opt/homebrew/lib", NULL};
173173
#else
174174
const char* standard_paths[] = {"/usr/lib", "/lib", "/usr/local/lib", NULL};
175175
#endif
@@ -378,6 +378,36 @@ static bool FindInLdSoConfFile(const char* conf_file, const char* library_name,
378378
}
379379
#endif // !_WIN32
380380

381+
#ifdef _WIN32
382+
#include <windows.h>
383+
#else
384+
#include <dlfcn.h>
385+
#endif
386+
387+
static bool JustTryDlOpenOnBasename(const char* library_name, char* resolved_path) {
388+
// In case our attempts to find the library fail, we can try to load it directly. Sometimes the OS might find it in cache.
389+
390+
// Attempt to load the library using dlopen
391+
#ifdef _WIN32
392+
void* handle = LoadLibrary(library_name);
393+
#else
394+
void* handle = dlopen(library_name, RTLD_LAZY);
395+
#endif
396+
if (!handle) {
397+
return false;
398+
}
399+
// If we successfully loaded the library, we can use the library_name directly since it was in fact found that way.
400+
strncpy(resolved_path, library_name, MAX_PATH_LENGTH);
401+
resolved_path[MAX_PATH_LENGTH - 1] = '\0'; // Ensure null-termination
402+
// Close the library handle. There's a reference counting mechanism in place, so this is safe.
403+
#ifdef _WIN32
404+
FreeLibrary(handle);
405+
#else
406+
dlclose(handle);
407+
#endif
408+
return true;
409+
}
410+
381411
static bool FindSharedLibrary(const char* library_name, char* resolved_path) {
382412

383413
if (!library_name || library_name[0] == '\0') {
@@ -419,19 +449,28 @@ static bool FindSharedLibrary(const char* library_name, char* resolved_path) {
419449
return false;
420450
}
421451
#endif
452+
else {
453+
454+
#ifdef _WIN32
455+
bool found = FindInEnvVar("PATH", library_name, resolved_path);
456+
#else
457+
bool found = FindInEnvVar("LD_LIBRARY_PATH", library_name, resolved_path)
458+
#ifdef use_ld_so_conf
459+
|| FindInLdSoConfFile("/etc/ld.so.conf", library_name, resolved_path, 0)
460+
#endif
461+
|| FindInStandardPaths(library_name, resolved_path);
462+
#endif
463+
464+
if (!found) {
465+
found = JustTryDlOpenOnBasename(library_name, resolved_path);
466+
}
467+
return found;
468+
}
469+
}
470+
471+
422472

423-
#ifdef _WIN32
424-
bool found = FindInEnvVar("PATH", library_name, resolved_path);
425-
#else
426-
bool found = FindInEnvVar("LD_LIBRARY_PATH", library_name, resolved_path)
427-
#ifdef use_ld_so_conf
428-
|| FindInLdSoConfFile("/etc/ld.so.conf", library_name, resolved_path, 0)
429-
#endif
430-
|| FindInStandardPaths(library_name, resolved_path);
431-
#endif
432473

433-
return found;
434-
}
435474

436475
// Function to attempt to resolve the library path
437476
char* resolve_library_path(const char* library_name) {

prepare_and_test_via_adb.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
adb push /work/build/cliffi /work/build/libcliffi_test.so /data/local/tmp
22
cd /work/build; ctest
3-
ctest --output-on-failure || ctest --rerun-failed --output-on-failure --extra-verbose
3+
ctest --output-on-failure --parallel || ctest --rerun-failed --output-on-failure --extra-verbose

0 commit comments

Comments
 (0)