C++: Unable to read resource files in macos bundle - macos

Background:
I was writing a "Hello Rectangle" OpenGL application. I want it to be a standalone .app instead of just a "UNIX executable." The code runs fine from Terminal.app.
I didn't use Xcode; I'm very tight on disk space and I'm not doing iOS dev.
Instead I'm using brewed toolchain, including gcc-8, glfw and glew.
I structured my release dir according to this SO question as follows:
Contents
├── Frameworks
│   ├── libGLEW.2.1.dylib
│   ├── libgcc_s.1.dylib
│   ├── libglfw.3.dylib
│   └── libstdc++.6.dylib
├── MacOS
│   ├── hello-rect <- the actual binary
│   └── launcher.sh <- entry wrapper for working dir as the above link suggests
├── Resources
│   ├── rect.frag
│   └── rect.vert
└── Info.plist
I also modified the reference to dylibs in binary. But it won't run.
After a few hours of Googling, this SO answer and this SO question and several others convinced me that I should use CoreFoundation to read those two shader files (text file) in Resources.
Problem:
I tried following code (pasted from the link above with slight modification), but it gets segfault:
const char* textFileReadCF(const char *&filename){
// Get a reference to the main bundle
CFBundleRef mainBundle = CFBundleGetMainBundle();
// Get a reference to the file's URL
CFStringRef filenameHandle = CFStringRef(filename);
// ------ segfault 11 here ------
CFURLRef fileURL = CFBundleCopyResourceURL(mainBundle, filenameHandle, NULL, NULL);
// Convert the URL reference into a string reference
CFStringRef filePath = CFURLCopyFileSystemPath(fileURL, kCFURLPOSIXPathStyle);
// Get the system encoding method
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
// Convert the string reference into a C string
const char *path = CFStringGetCStringPtr(filePath, encodingMethod);
// the real read function, read in file and returns all as a big char* arr
return textFileRead(path);
}
// this runs fine before packing and I didn't change,
// just for the sake of completeness
const char *textFileRead(const char *filename){
std::ifstream shaderFile(filename);
std::ostringstream shaderBuffer;
shaderBuffer << shaderFile.rdbuf();
std::string shaderBufferStr = shaderBuffer.str();
// Warning: safe only until shaderBufferStr is destroyed or modified
char * ret = new char[shaderBufferStr.size()];
std::strcpy(ret, shaderBufferStr.c_str());
return ret;
}
textFileReadCF will be called twice with "rect.vert" and "rect.vert" (not literal, but const char* holding them).
I have no idea why it goes segfault. I made sure there is one mainBundle through assert (omitted), and that filenameHandle is valid as I can tell, but otherwise I don't know where to look.

Turns out I do not need CoreFoundation. I just need GLFW.
In an earlier version, my release dir looked like this:
Contents
├── Frameworks
│ ├── libGLEW.2.1.dylib
│ ├── libgcc_s.1.dylib
│ ├── libglfw.3.dylib
│ └── libstdc++.6.dylib
├── MacOS
│ ├── hello-rect <- the actual binary
│ ├── launcher.sh <- entry wrapper for working dir as the above link suggests
│ ├── rect.frag
│ └── rect.vert
└── Info.plist
Because the unbundled version obviously looks nowhere but cwd.
I didn't even know there is Resources. At that time my code also didn't run; my Google trace let me believe that CoreFoundation is the solution. Only until I checked docs about CoreFoundation did I know there should be Resources. And only by chance did I come across this SO question, when all the dots connected.
Apparently my version of GLFW also behaves like the link states: (directly from source)
// Change to our application bundle's resources directory, if present
So all it takes to make the bundle run out of box is, just structure the release dir like in question, and directly call textFileRead with "rect.vert" and "rect.vert", and we're all set.

Related

Bash: automatically add a file to a Xcode project?

I am creating a script.sh file that creates a Test.swift file and adds it into a Xcode project. However, I would like to know if there is a way to add this file to Xcode (in the project.pbxproj file) from this script? Instead of doing it manually in Xcode (Add files to Project...).
Thank you
3/05 Update
I tried #Johnykutty answer, here is my current Xcode project before executing the ruby script:
I have already generated a A folder with a Sample.swift file located in test, but these files are not linked to my Xcode project yet:
Now here is the script that I'm executing:
require 'xcodeproj'
project_path = '../TestCodeProjTest.xcodeproj'
project = Xcodeproj::Project.open(project_path)
file_group = project["TestCodeProjTest"]["test"]
file_group.new_file("#{project.project_dir}/TestCodeProjTest/test/A")
project.save()
This almost works fine, except that it creates a folder reference instead of a group, and it doesn't link it to my target:
Hence the content of Sample.swift is unreachable.
Its hard to achieve by bash. But really easy if you use Ruby and xcodeproj gem from Cocoapods
Consider you have file structure like
├── GeneratedFiles
│   └── Sample1.swift
├── MyProject
│   ├── AppDelegate.swift
│   ├── ... all other files
│   ├── SceneDelegate.swift
│   └── ViewController.swift
├── MyProject.xcodeproj
│   ├── project.pbxproj
│   ├── .....
└── add_file.rb
Then you can add files like
require 'xcodeproj'
project_path = 'MyProject.xcodeproj'
project = Xcodeproj::Project.open(project_path)
file_group = project["MyProject"]
file_group.new_file("../GeneratedFiles/Sample1.swift")
project.save()
UPDATE:
project["MyProject"] returns a file group which is a group named MyProject in the root of the project, you can select another group inside MyProject by file_group = project["MyProject"]["MyGroup"]
Then the generated file path should be either related to that group like file_group.new_file("../../GeneratedFiles/Sample1.swift") or full path like file_group.new_file("#{project.project_dir}/GeneratedFiles/Sample1.swift")
More details about Xcodeproj here

Use of internal package not allowed on Windows 10 Go v1.12.5

Yesterday I helped a colleague install go on his Windows 10 PC. We downloaded and installed the latest (v1.12.5) from golang.org. No errors were reported during the install. We ran into problems trying to get and build a private project we're collaborating on.
The first problem was trying to use go get to fetch the project into the go/src tree. It prompted for his GitHub password and appeared to be downloading but failed with a complaint that the project contained no Go files. So we git cloned the project directly under C:\go\src. That succeeded, but attempting to build produces complaints about "use of internal package not allowed".
There are multiple prior reports of this error message on SO (e.g. golang disable "use of internal package not allowed") and as issues in golang/go (e.g. https://github.com/golang/go/issues/26446) but most of the discussions explain the problem as having to do with attempts to import from internals of packages with repositories outside the project root. That's not the case here.
The project is pure Go and all of the code in the repo builds successfully on OS-X and Linux.
For the above reasons, I believe this question is not a duplicate.
For reference, here is a much reduced view of the project directory installed under the go/src tree showing one of the files that produces the error when attempting to run go build. In this case the complaint references the internal/ace/ package as the disallowed import but the problem also occurs in other subdirectories (not shown) that import from other internal packages (also not shown.)
go
└── src
├── github.com
│   ├── Michael-F-Ellis
│   │   ├── pgcgo
│   │   │   ├── internal
│   │   │   │ ├── ace
│   │   │   ├── setacertc
│   │   │   ├ ├── main.go
Here is the outline of setacertc/main.go:
// setacertc is intended to be run from an internet-connected host to
// set the real-time-clock on the ACE11.
package main
import (
"fmt"
"log"
"math/rand"
"time"
"github.com/Michael-F-Ellis/pgcgo/internal/ace"
)
func main() {
// SNIP
}
I suspect the problem is somehow specific to a new installation and/or running Go under Windows. Any help appreciated.

Building Play Framework 2.4 application with Gradle 2.6 FAILED

everyone!
The main problem is that I fail to build my Play Framework 2.4.0 application with Gradle 2.6.
The following is my build.gradle file (nothing special, everything here is from the official docs on using gradle with play framework https://docs.gradle.org/current/userguide/play_plugin.html):
plugins {
id 'play'
}
repositories {
jcenter()
maven {
name "typesafe-maven-release"
url "https://repo.typesafe.com/typesafe/maven-releases"
}
ivy {
name "typesafe-ivy-release"
url "https://repo.typesafe.com/typesafe/ivy-releases"
layout "ivy"
}
mavenCentral()
}
model {
components {
play {
platform play: '2.4.0'
}
}
}
I used playBinary, runPlayBinary and the composite tasks one by one (such as compilePlayBinaryRoutes, compilePlayBinaryTwirlTemplates and compilePlayBinaryScala), however the result is essentially the same every time:
~/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI$ gradle playBinary
:compilePlayBinaryRoutes UP-TO-DATE
:compilePlayBinaryTwirlTemplates UP-TO-DATE
:compilePlayBinaryScala
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryRoutes/router/Routes.scala:56: value index is not a member of object controllers.Application
controllers.Application.index(),
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryRoutes/router/Routes.scala:73: value updateSettings is not a member of object controllers.Application
controllers.Application.updateSettings(),
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryRoutes/router/Routes.scala:107: value getResource is not a member of object controllers.Application
controllers.Application.getResource(fakeValue[String]),
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[String]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[String]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[String]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[Long]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:158: value get is not a member of List[String]
document.getElementById('timeLength').value = '"""),_display_(/*140.73*/timeLengths/*140.84*/.get(1)),format.raw/*140.91*/("""'""")))}/*140.94*/else/*140.99*/{_display_(Seq[Any](format.raw/*140.100*/("""
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:182: value get is not a member of List[String]
"""),format.raw/*164.41*/("""<span title=""""),_display_(/*164.55*/keysToParse/*164.66*/.get(i + 1)),format.raw/*164.77*/("""" class=""""),_display_(/*164.87*/abbreviations/*164.100*/.get(i)),format.raw/*164.107*/("""_"""),_display_(/*164.109*/i),format.raw/*164.110*/(""""><input type="checkbox" id=""""),_display_(/*164.140*/i),format.raw/*164.141*/("""" checked onclick="change(this)">"""),_display_(/*164.175*/abbreviations/*164.188*/.get(i)),format.raw/*164.195*/("""</span>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:182: value get is not a member of List[String]
"""),format.raw/*164.41*/("""<span title=""""),_display_(/*164.55*/keysToParse/*164.66*/.get(i + 1)),format.raw/*164.77*/("""" class=""""),_display_(/*164.87*/abbreviations/*164.100*/.get(i)),format.raw/*164.107*/("""_"""),_display_(/*164.109*/i),format.raw/*164.110*/(""""><input type="checkbox" id=""""),_display_(/*164.140*/i),format.raw/*164.141*/("""" checked onclick="change(this)">"""),_display_(/*164.175*/abbreviations/*164.188*/.get(i)),format.raw/*164.195*/("""</span>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:182: value get is not a member of List[String]
"""),format.raw/*164.41*/("""<span title=""""),_display_(/*164.55*/keysToParse/*164.66*/.get(i + 1)),format.raw/*164.77*/("""" class=""""),_display_(/*164.87*/abbreviations/*164.100*/.get(i)),format.raw/*164.107*/("""_"""),_display_(/*164.109*/i),format.raw/*164.110*/(""""><input type="checkbox" id=""""),_display_(/*164.140*/i),format.raw/*164.141*/("""" checked onclick="change(this)">"""),_display_(/*164.175*/abbreviations/*164.188*/.get(i)),format.raw/*164.195*/("""</span>
^
11 errors found
:compilePlayBinaryScala FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compilePlayBinaryScala'.
> Compilation failed
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 8.445 secs
And here's the structure of the build directory, after build failure:
build
├── playBinary
│   ├── classes
│   └── src
│   ├── compilePlayBinaryRoutes
│   │   ├── controllers
│   │   │   ├── javascript
│   │   │   │   └── JavaScriptReverseRoutes.scala
│   │   │   ├── ReverseRoutes.scala
│   │   │   └── routes.java
│   │   └── router
│   │   ├── RoutesPrefix.scala
│   │   └── Routes.scala
│   └── compilePlayBinaryTwirlTemplates
│   └── views
│   └── html
│   └── index.template.scala
└── tmp
└── compilePlayBinaryScala
My guess is that it might have something to do with the fact, that Gradle 2.6 doesn't support reverse routing for now. I tried creating a new Play application (2.4.2 this time) and built it straight away, however it also failed on the same part:
controllers.Application.index(),
^
So let's go one by one:
All the errors after the first 3 were my mistakes, but I somehow assumed, that they were caused by the first 3. All of those were basically the result of not proper treating of the data structures in scala (my bad, I'm a noob there).
The first 3 however were caused by the fact, that I had non-static methods in my main Controller. Somehow, I must have overlooked the fact, that you have to prefix your non-static methods calls in routes file with a '#' sign. So the solution is to either place the prefix in the routes file, or make methods static.
The only reference to this (static/non-static behavior), that I found is a scarce mentioning here (https://www.playframework.com/documentation/2.4.0/JavaRouting#Dependency-Injection [last line of this paragraph]).

Qmake configuration using Buildroot

I’ve tried to add a package to Buildroot that uses Qt and Boost. The package uses qmake to generate a Makefile, this part seems to be working, however I get an error when I build saying:
Could not find qmake configuration file qws/linux-arm-g++.
Error processing project file: MsgDisplay.pro
The contents of my package is laid out like this:
DummyPgm
├── main.cpp
├── MsgDisplay.pri
├── MsgDisplay.pro
├── MsgDisplay.pro.user
├── MsgHandler.cpp
├── MsgHandler.h
├── MsgServer.cpp
├── MsgServer.h
├── Tcp
│ ├── TcpAddrPort.cpp
│ ├── TcpAddrPort.h
│ ├── TcpServer.cpp
│ ├── TcpServer.h
│ ├── TcpSocket.cpp
│ └── TcpSocket.h
└── Tools
├── Banner.cpp
├── Banner.h
├── IoExt.h
├── SeparateArgumentList.cpp
├── SeparateArgumentList.h
└── SysTypes.h
2 directories, 20 files
I have added a package directory, dummypgm, which contains Config.in and dummypgm.mk files. The contents of the files are:
Config.in:
config BR2_PACKAGE_DUMMYPGM
bool "dummypgm"
help
Foo Software.
http://www.foo.com
dummypgm.mk:
DUMMYPGM_VERSION = 0.1.0
DUMMYPGM_SOURCE = DummyPgm-$(DUMMYPGM_VERSION).tar.gz
define DUMMYPGM_CONFIGURE_CMDS
(cd $(#D); $(QT_QMAKE) MsgDisplay.pro)
endef
define DUMMYPGM_BUILD_CMDS
$(MAKE) -C $(#D)
endef
$(eval $(generic-package))
Since the package is hosted locally, I’ve simply put the DummyPgm-0.1.0.tar.gz in the dl directory.
I’ve also added the following to package/Config.in:
source "package/dummypgm/Config.in"
I’m a little lost as to why this doesn’t work, if anyone could help me I would be very grateful. Also, is there any way to call $(eval $(qmake-package)) or something?
Are you using Qt4 or Qt5 ? Your package/dummypgm/Config.in should have a depends on on one of them, and your dummypgm.mk should have a DUMMYPGM_DEPENDENCIES = qt or DUMMYPGM_DEPENDENCIES = qt5base.
My intuition is that you are using Qt5. In this case, you shouldn't call $(QT_QMAKE), but $(QT5_QMAKE).
Have a look at http://git.buildroot.net/buildroot/tree/package/qextserialport/qextserialport.mk for an example. Note that this example supports both Qt4 and Qt5, probably in your case you only need one of the two.
Also, you should really subscribe to the Buildroot mailing list, you would get a lot more answers than here.

Classes defined in CoffeeScript not found by Jasmine specs

I am building a backbone.js app on a Rails 3.1 back-end. I'm using CoffeeScript to write the backbone classes, and Jasmine (via jasmine-headless-webkit) for testing.
Given the following (partial) tree:
.
├── app
│   ├── assets
│   │   ├── javascripts
│   │   │   └── views
│   │   │   ├── avia_view.js.coffee
├── spec
│   ├── javascripts
│   │   └── views
│   │   └── avia_view_spec.js.coffee
... I would expect avia_view_spec.js.coffee to know about Avia.AviaView, which is defined in avia_view.js.coffee.
However, I get the following output from running bundle exec jasmine-headless-webkit:
Running Jasmine specs...
F
Avia.AviaView render creates a new MatricesView . (/home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee:10)
ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~5)
ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~10)
My jasmine.yml contains the following:
src_files:
- public/javascripts/prototype.js
- public/javascripts/effects.js
- public/javascripts/controls.js
- public/javascripts/dragdrop.js
- public/javascripts/application.js
- public/javascripts/**/*.js
I think I need to tell Jasmine to load the contents of avia_view.js.coffee but I'm not entirely sure how. Adding an explicit reference in the src_files section in jasmine.yml doesn't seem to make a difference ...
Could someone please tell me what I'm doing wrong here? I suspect it's something simple ...
Without having seen to much of your code, I'd suspect it beacuse of CoffeeScript's function wrapping (docs). You need to ensure that all the symbols you want to use are exported to somewhere you can get at them (here is a thorough discussion about that).
Edit: here's another longish and theoretical but good documentation on this topic.
Try adding this to your avia_view.js.coffee
(exports ? this).Avia = Avia
See this for a detailed explanation.
Alternatively try this;
window.Avia = Avia
We encountered the same problem; I highly recommend JasmineRice

Resources