Skip to content

Instantly share code, notes, and snippets.

@floooh
Last active February 24, 2016 18:03
Show Gist options
  • Save floooh/3b8a475d840b07d11402 to your computer and use it in GitHub Desktop.
Save floooh/3b8a475d840b07d11402 to your computer and use it in GitHub Desktop.
chdir() in OSX app bundles vs cmd line executables

Ok, about the problem that the bgfx samples built with fips-bgfx cannot load the sample data from the examples/runtime directory even though the working directory at application startup is set to that directory (see here: floooh/fips-bgfx#7). When building with the original bgfx build files, everything works fine. The only difference seems to be that bgfx builds the samples as app bundles, but fips-bgfx builds the samples as 'raw' command line exe.

There is a chdir() in the bgfx sample entry code which is supposed to change the cwd to the app bundle's resource directory (since this is where OSX resources usually are). Now the interesting thing is, that this is chdir() is ignored when the app is compiled as app bundle, and the originally set working directory (examples/runtime) is preserved. But when compiling as command line exe (like fips-bgfx does), the chdir works, changes the working directory to the directory where the executable lives (away from examples/runtime), and loading the data fails.

I've put a getcwd() before and after the hdir() here: https://github.com/bkaradzic/bgfx/blob/master/examples/common/entry/entry_osx.mm#L68,

So the debugging code looks like this:

char buf[1024];
getcwd(buf, sizeof(buf));
printf("before: %s\n", buf);
char path[PATH_MAX];
if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX) )
{
	printf("chdir: %s\n", path);
	chdir(path);
	getcwd(buf, sizeof(buf));
	printf("after: %s\n", buf);
...

When debugging the original Genie build (app bundle):

before: /Users/floh/projects/fips-bgfx/bgfx/examples/runtime
chdir: /Users/floh/projects/fips-bgfx/bgfx/.build/osx_universal/bin/example-27-terrainDebug.app/Contents/Resources
after: /Users/floh/projects/fips-bgfx/bgfx/examples/runtime

In fips-bgfx as cmdline exe:

before: /Users/floh/projects/fips-bgfx/bgfx/examples/runtime
chdir: /Users/floh/projects/fips-deploy/fips-bgfx/osx-xcode-debug
after: /Users/floh/projects/fips-deploy/fips-bgfx/osx-xcode-debug

And fips-bgfx when compiled as app-bundle (same behaviour as Genie build):

before: /Users/floh/projects/fips-bgfx/bgfx/examples/runtime
chdir: /Users/floh/projects/fips-deploy/fips-bgfx/osx-xcode-debug/27-terrain.app/Contents/Resources
after: /Users/floh/projects/fips-bgfx/bgfx/examples/runtime

So we could get the same behaviour in fips-bgfx by building the samples as windowed apps (which on Mac results in app bundles). But I think the entire chdir() stuff in the bgfx sample wrapper startup code doesn't work as intended.

@maljx suggested on Twitter that it could be an app sandbox 'feature', but the sandbox setting is switched off, so I'm pretty much out of ideas why chdir() wouldn't work in an app bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment