Skip to content

Commit

Permalink
Cross Platform Exe Path
Browse files Browse the repository at this point in the history
This change integrates the library "whereami" to determine the path of the
running executable.  See https://github.com/gpakosz/whereami for more details.

This allows us to have consistent behavior across all build configurations.

I also added a fallback case for when it fails on Linux, to add an additional
hint if the proc filesystem is missing.  This may be useful to help people
attempting to run Tangerine from an improperly configured chroot environment
without downgrading to behavior that might be less secure such as using
argv 0 as a fallback.
  • Loading branch information
Aeva committed Feb 24, 2023
1 parent 47d46ee commit 24cfebf
Show file tree
Hide file tree
Showing 31 changed files with 2,572 additions and 40 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ target_sources(VoxWriter
PUBLIC FILE_SET HEADERS
FILES "third_party/voxwriter/VoxWriter.h")

#######################################################################
## whereami:

add_library(whereami OBJECT "third_party/whereami/src/whereami.c")
target_include_directories(whereami PUBLIC "third_party/whereami/src/")


#######################################################################
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##
Expand All @@ -151,6 +157,7 @@ target_link_libraries(tangerine PRIVATE
imgui
ImFileDialog
VoxWriter
whereami
tinfo # see https://gitlab.kitware.com/cmake/cmake/-/issues/23236
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_DL_LIBS})
Expand Down
55 changes: 20 additions & 35 deletions tangerine/installation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,35 @@
// limitations under the License.

#include "installation.h"

#if !_WIN64
#include <unistd.h>
#endif
#include "whereami.h"
#include <iostream>


TangerinePaths::TangerinePaths(int argc, char* argv[])
StatusCode TangerinePaths::PopulateInstallationPaths()
{
#if _WIN64
// TODO: This is fine for standalone builds, but we will want to do something
// else for future library builds. Maybe GetModuleFileNameW?
ExecutablePath = std::filesystem::absolute(argv[0]);

#elif EMBED_RACKET
ExecutablePath = std::filesystem::path(racket_get_self_exe_path(argv[0]));

#else
{
char* Path = nullptr;
ssize_t PathLength = 0;
ssize_t Allocation = 256;

retry:
Path = (char*)malloc(Allocation);
PathLength = readlink("/proc/self/exe", Path, Allocation - 1);
if (PathLength == (Allocation - 1))
int Length = wai_getExecutablePath(NULL, 0, NULL);
if (Length > -1)
{
char* Path = (char*)malloc(Length + 1);
wai_getExecutablePath(Path, Length, NULL);
Path[Length] = '\0';
ExecutablePath = std::filesystem::path(Path);
free(Path);
Allocation *= 2;
goto retry;
}

if (PathLength < 0)
{
// Possibly in a chroot environment where "/proc" is not available, so fall back to generic approach.
ExecutablePath = std::filesystem::absolute(argv[0]);
}
else
{
Path[PathLength] = 0;
ExecutablePath = std::filesystem::path(Path);
#if !_WIN64
// TODO: Right now our platform coverage is "Windows" and "not Windows == Linux". More nuance may be needed here in the future.
if (!std::filesystem::exists("/proc"))
{
std::cout << "Proc filesystem not found.\n";
}
#endif
std::cout << "Failed to determine Tangerine's filesystem path.\n";
return StatusCode::FAIL;
}

free(Path);
}
#endif

ExecutableDir = ExecutablePath.parent_path();

Expand All @@ -75,4 +58,6 @@ TangerinePaths::TangerinePaths(int argc, char* argv[])

ShadersDir = PkgDataDir / std::filesystem::path("shaders");
ModelsDir = PkgDataDir / std::filesystem::path("models");

return StatusCode::PASS;
}
4 changes: 2 additions & 2 deletions tangerine/installation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
#pragma once
#include <filesystem>
#include "embedding.h"
#include "errors.h"


struct TangerinePaths
{
TangerinePaths() {}
TangerinePaths(int argc, char* argv[]);
StatusCode PopulateInstallationPaths();

std::filesystem::path ExecutablePath;
std::filesystem::path ExecutableDir;
Expand Down
2 changes: 1 addition & 1 deletion tangerine/tangerine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,7 @@ SDL_GLContext Context = nullptr;

StatusCode Boot(int argc, char* argv[])
{
Installed = TangerinePaths(argc, argv);
RETURN_ON_FAIL(Installed.PopulateInstallationPaths());
LastOpenDir = Installed.ModelsDir;
LoadBookmarks();

Expand Down
16 changes: 16 additions & 0 deletions third_party/licenses.inl
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,19 @@ if (ImGui::BeginTabItem("voxwriter"))
ImGui::EndTabItem();
}
#endif

#if 1
if (ImGui::BeginTabItem("whereami"))
{
ImGui::TextUnformatted("MIT License\n\nCopyright Gregory Pakosz\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", nullptr);
ImGui::EndTabItem();
}
#endif

#if 1
if (ImGui::BeginTabItem("whereami"))
{
ImGui::TextUnformatted("--------------------------------------------------------------------------------\n DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE\n Version 2, December 2004\n\n Copyright (C) 2004 Sam Hocevar <[email protected]>\n\n Everyone is permitted to copy and distribute verbatim or modified\n copies of this license document, and changing it is allowed as long\n as the name is changed.\n\n DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE\n TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n\n 0. You just DO WHAT THE FUCK YOU WANT TO.\n 1. Bla bla bla\n 2. Montesqieu et camembert, vive la France, zut alors!\n\n--------------------------------------------------------------------------------\n\nWTFPLv2 is very permissive, see http://www.wtfpl.net/faq/\n\nHowever, if this WTFPLV2 is REALLY a blocker and is the reason you can't use\nthis project, contact me and I'll dual license it.\n\n--------------------------------------------------------------------------------\n", nullptr);
ImGui::EndTabItem();
}
#endif
20 changes: 20 additions & 0 deletions third_party/whereami/LICENSE.MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright Gregory Pakosz

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions third_party/whereami/LICENSE.WTFPLv2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--------------------------------------------------------------------------------
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
1. Bla bla bla
2. Montesqieu et camembert, vive la France, zut alors!

--------------------------------------------------------------------------------

WTFPLv2 is very permissive, see http://www.wtfpl.net/faq/

However, if this WTFPLV2 is REALLY a blocker and is the reason you can't use
this project, contact me and I'll dual license it.

--------------------------------------------------------------------------------
123 changes: 123 additions & 0 deletions third_party/whereami/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Where Am I?

A drop-in two files library to locate the current executable and the current
module on the file system.

Supported platforms:

- Windows
- Linux
- Mac
- iOS
- Android
- QNX Neutrino
- FreeBSD
- NetBSD
- DragonFly BSD
- SunOS
- OpenBSD

Just drop `whereami.h` and `whereami.c` into your build and get started. (see
also [customizing compilation])

[customizing compilation]: #customizing-compilation

--------------------------------------------------------------------------------

## Usage

- `wai_getExecutablePath()` returns the path of the enclosing executable
- `wai_getModulePath()` returns the path of the enclosing module

Example usage:

- first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to retrieve
the length of the path
- allocate the destination buffer with `path = (char*)malloc(length + 1);`
- call `wai_getExecutablePath(path, length, &dirname_length)` again to retrieve
the path
- add a terminal `NUL` character with `path[length] = '\0';`

Here is the output of the example:

$ make -j -C _gnu-make
$ cp ./bin/mac-x86_64/library.dylib /tmp/
$ ./bin/mac-x86_64/executable --load-library=/tmp/library.dylib

executable path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable
dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64
basename: executable
module path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable
dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64
basename: executable

library loaded
executable path: /Users/gregory/Projects/whereami/bin/mac-x86_64/executable
dirname: /Users/gregory/Projects/whereami/bin/mac-x86_64
basename: executable
module path: /private/tmp/library.dylib
dirname: /private/tmp
basename: library.dylib
library unloaded

--------------------------------------------------------------------------------

## Customizing compilation

You can customize the library's behavior by defining the following macros:

- `WAI_FUNCSPEC`
- `WAI_PREFIX`
- `WAI_MALLOC`
- `WAI_REALLOC`
- `WAI_FREE`

## Compiling for Windows

There is a Visual Studio 2015 solution in the `_win-vs14/` folder.

## Compiling for Linux or Mac

There is a GNU Make 3.81 `MakeFile` in the `_gnu-make/` folder:

$ make -j -C _gnu-make/

## Compiling for Mac

See above if you want to compile from command line. Otherwise there is an Xcode
project located in the `_mac-xcode/` folder.

## Compiling for iOS

There is an Xcode project located in the `_ios-xcode/` folder.

If you prefer compiling from command line and deploying to a jailbroken device
through SSH, use:

$ 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'"

## Compiling for Android

You will have to install the Android NDK, and point the `$NDK_ROOT` environment
variable to the NDK path: e.g. `export NDK_ROOT=/opt/android-ndk` (without a
trailing `/` character).

Next, the easy way is to make a standalone Android toolchain with the following
command:

$ $NDK_ROOT/build/tools/make_standalone_toolchain.py --arch=arm64 --api 21 --install-dir=/tmp/android-toolchain

Now you can compile the example by running:

$ 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++

Loading page aligned library straight from APKs is supported. To test, use the
following:

$ zip -Z store app bin/android/library.so
$ zipalign -v -f -p 4 ./app.zip ./app.apk

Then copy `bin/android/executable` and `app.apk` to your Android device and
there launch:

$ ./executable --load-library=$PWD/app.apk!/bin/android/library.so
Loading

0 comments on commit 24cfebf

Please sign in to comment.