|
| 1 | +# Where Am I? |
| 2 | + |
| 3 | +A drop-in two files library to locate the current executable and the current |
| 4 | +module on the file system. |
| 5 | + |
| 6 | +Supported platforms: |
| 7 | + |
| 8 | +- Windows |
| 9 | +- Linux |
| 10 | +- Mac |
| 11 | +- iOS |
| 12 | +- Android |
| 13 | +- QNX Neutrino |
| 14 | +- FreeBSD |
| 15 | +- NetBSD |
| 16 | +- DragonFly BSD |
| 17 | +- SunOS |
| 18 | +- OpenBSD |
| 19 | + |
| 20 | +Just drop `whereami.h` and `whereami.c` into your build and get started. (see |
| 21 | +also [customizing compilation]) |
| 22 | + |
| 23 | +[customizing compilation]: #customizing-compilation |
| 24 | + |
| 25 | +-------------------------------------------------------------------------------- |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +- `wai_getExecutablePath()` returns the path of the enclosing executable |
| 30 | +- `wai_getModulePath()` returns the path of the enclosing module |
| 31 | + |
| 32 | +Example usage: |
| 33 | + |
| 34 | +- first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to retrieve |
| 35 | + the length of the path |
| 36 | +- allocate the destination buffer with `path = (char*)malloc(length + 1);` |
| 37 | +- call `wai_getExecutablePath(path, length, &dirname_length)` again to retrieve |
| 38 | + the path |
| 39 | +- add a terminal `NUL` character with `path[length] = '\0';` |
| 40 | + |
| 41 | +Here is the output of the example: |
| 42 | + |
| 43 | + $ make -j -C _gnu-make |
| 44 | + $ cp ./bin/mac-x86_64/library.dylib /tmp/ |
| 45 | + $ ./bin/mac-x86_64/executable --load-library=/tmp/library.dylib |
| 46 | + |
| 47 | + executable path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable |
| 48 | + dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64 |
| 49 | + basename: executable |
| 50 | + module path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable |
| 51 | + dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64 |
| 52 | + basename: executable |
| 53 | + |
| 54 | + library loaded |
| 55 | + executable path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable |
| 56 | + dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64 |
| 57 | + basename: executable |
| 58 | + module path: /private/tmp/library.dylib |
| 59 | + dirname: /private/tmp |
| 60 | + basename: library.dylib |
| 61 | + library unloaded |
| 62 | + |
| 63 | +-------------------------------------------------------------------------------- |
| 64 | + |
| 65 | +## Customizing compilation |
| 66 | + |
| 67 | +You can customize the library's behavior by defining the following macros: |
| 68 | + |
| 69 | +- `WAI_FUNCSPEC` |
| 70 | +- `WAI_PREFIX` |
| 71 | +- `WAI_MALLOC` |
| 72 | +- `WAI_REALLOC` |
| 73 | +- `WAI_FREE` |
| 74 | + |
| 75 | +## Compiling for Windows |
| 76 | + |
| 77 | +There is a Visual Studio 2015 solution in the `_win-vs14/` folder. |
| 78 | + |
| 79 | +## Compiling for Linux or Mac |
| 80 | + |
| 81 | +There is a GNU Make 3.81 `MakeFile` in the `_gnu-make/` folder: |
| 82 | + |
| 83 | + $ make -j -C _gnu-make/ |
| 84 | + |
| 85 | +## Compiling for Mac |
| 86 | + |
| 87 | +See above if you want to compile from command line. Otherwise there is an Xcode |
| 88 | +project located in the `_mac-xcode/` folder. |
| 89 | + |
| 90 | +## Compiling for iOS |
| 91 | + |
| 92 | +There is an Xcode project located in the `_ios-xcode/` folder. |
| 93 | + |
| 94 | +If you prefer compiling from command line and deploying to a jailbroken device |
| 95 | +through SSH, use: |
| 96 | + |
| 97 | + $ make -j -C _gnu-make/ binsubdir=ios CC="$(xcrun --sdk iphoneos --find clang) -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -arch armv7 -arch armv7s -arch arm64" postbuild="codesign -s 'iPhone Developer'" |
| 98 | + |
| 99 | +## Compiling for Android |
| 100 | + |
| 101 | +You will have to install the Android NDK, and point the `$NDK_ROOT` environment |
| 102 | +variable to the NDK path: e.g. `export NDK_ROOT=/opt/android-ndk` (without a |
| 103 | +trailing `/` character). |
| 104 | + |
| 105 | +Next, the easy way is to make a standalone Android toolchain with the following |
| 106 | +command: |
| 107 | + |
| 108 | + $ $NDK_ROOT/build/tools/make_standalone_toolchain.py --arch=arm64 --api 21 --install-dir=/tmp/android-toolchain |
| 109 | + |
| 110 | +Now you can compile the example by running: |
| 111 | + |
| 112 | + $ make -j -C _gnu-make/ platform=android architecture=arm64 CC=/tmp/android-toolchain/bin/aarch64-linux-android-gcc CXX=/tmp/android-toolchain/bin/aarch64-linux-android-g++ |
| 113 | + |
| 114 | +Loading page aligned library straight from APKs is supported. To test, use the |
| 115 | +following: |
| 116 | + |
| 117 | + $ zip -Z store app bin/android/library.so |
| 118 | + $ zipalign -v -f -p 4 ./app.zip ./app.apk |
| 119 | + |
| 120 | +Then copy `bin/android/executable` and `app.apk` to your Android device and |
| 121 | +there launch: |
| 122 | + |
| 123 | + $ ./executable --load-library=$PWD/app.apk!/bin/android/library.so |
0 commit comments