Add source code to Xcode target programmatically - ios

I'm using protobuf to generate swift classes from protofiles (I've written a script for this purpose). Now I need to add all generated files into target in Xcode project.
How can I do this programmatically? Maybe script in Build Phases or some other kind of actions in macOS (Automator?)?

CocoaPods uses this project to modify project files: https://github.com/CocoaPods/Xcodeproj

It is possible to do that with functionality built into Xcode (we have done it with c++, but it should be possible with swift, too):
In your target, add a custom shell script build rule that matches the extension of your protofiles.
Call your script using "${INPUT_FILE_PATH}" and "${SCRIPT_OUTPUT_FILE_0}".
If the output file's extension matches another build rule, that one will be executed. So in you case, set the output file of the build rule to ${DERIVED_FILE_DIR}/${INPUT_FILE_BASE}.swift in order to send it to the swift compiler.
Now go to your target's Compile Sources build phase and add the protofiles.

You can do this via Xcode "Build Rules"
Check this project which leverage "Build Rules" to generate source code files and add them to the project from protobuf
https://github.com/microsoft/plcrashreporter

Related

Force Xcode to build with specific .framework

I have a target that links with a framework (both of which I develop). If I build the target it builds the framework before hand for use in the target.
What I want during certain builds is to bring in an existing already built version of that framework and use it during the build of that target. Is there a way to force xcode to build using an already compiled version of a dependant framework
(The reason I'm doing this is because I have multiple targets that are built separately and want them all to be built using the same framework binary)
I finally solved this, this can be done by turning off the "Find Implicit Dependencies" under your build scheme.
Some good reading can be found here on the topic here: https://pewpewthespells.com/blog/managing_xcode.html#scheme-action

Dynamically link binaries with library in xcode

To link binaries to you framework you can from build phase add your needed libraries to your project .. but what if I need to make some thing specific for example I need to customise my framework by adding or deleting binaries.. foe example I want lite version and full version so I need always to delete and add these binaries from "Link binary.." tab
Is there any dynamic way like to make different configuration files or some thing?
You'd need to create 2 Xcode targets, one for lite and one for pro, which compile the same source files and link to mostly the same frameworks etc.
You can also add compiler constants to the Build Settings of these targets, which will allow the common source files to behave differently between these target "products".

Script to add list of framework into xcode project? [duplicate]

I am looking for a way to add libraries to an Xcode project, using the command line.
I have been successful in adding files to groups with the XCS tool, but it does not support libraries.
I would, for example, like to be able to add CoreVideo.framework to a specific project with a command on the Terminal.
This project can handle frameworks:
https://github.com/kronenthaler/mod-pbxproj
Just add it as a normal file, it will figure out the correct type and how to set everything up (i.e., add it to the link library phase – before using it, you still need to import the header(s), of course).
// libFilePath: Path to the framework
frameworkGroup = project.get_or_create_group('Frameworks')
project.add_file(libFilePath,
parent=frameworkGroup,
tree='SDKROOT',
weak=True)
You can decide whether you want to weak-link frameworks (see code example) or not (just leave out the last parameter, it defaults to False). You can find the code to open/save Xcode projects in the repository.
Another way to do it is adding linker flags directly, e.g., -framework CoreVideo.framework. If the framework paths are set up correctly, you don't have to provide absolute paths. The disadvantage of this approach is that the linked frameworks aren't obvious if you open the Xcode project, as they are not part of the link library section, nor does the framework show up in any Xcode group in the Project Navigator.
You can try generating your .xcodeproj using tools like XcodeGen. It allows you to include different types of dependencies.

Any way to make bundle xcode project to include a static library xcode project?

I have a static library xcode project (.a) and a bundle xcode project (.bundle)
I added the (.a) as a sub project of (.bundle) and added to [Target Dependencies] and [Link Binary With Libraries].
After run the bulid the (.bundle) still not contain any binary file inside (.bundle).
It's work if I add the .c and .h files to under (.bundle) directly, but that make me need to handle two project files. Any way can make (.bundle) just build with the (.a) ?
This question same as what I asked, I tried to follow his 11 steps without the step 10 because he said lastly no need that step. But the generated (.bundle) still without contain any binary
Finally, I make it work.
The step 10 still important and below is corrected step 10.
create a dummy.c under (.bundle) project and the dummy.c can just totally empty. remove the setting for the library you want to link inside Link Binary With Libraries instead use -Wl,-force_load,$(CONFIGURATION_BUILD_DIR)/libYourLib.a or -all_load to Other Linker Flags
PS: And also can use sub-project instead of workspace. and use Target Dependencies instead of Edit Scheme to achieve the same effect.
The testing project

Running a Clang LibTooling tool over an iOS Xcode project

I've written a toy libtooling based tool that does some analysis/source rewriting over ObjectiveC code. How do I run it over an iOS Xcode project?
I've looked at compiling the application through commandline/clang, but I haven't got it to work yet. Is it possible to chain my tool with xcodebuild? Or is there a better way to run the tool over an Xcode project?
I think what you need is JSON-Compilation-Database.
You can use xctool to generate a json compilation databse (xctool has a reporter that generate this thing, see the readme.)
The json compilation database is just a json file which contains all the compiler options (say, the header search path, the frame work search path) that need to pass to the clang and libtooling based tools.
If you want your tool to generate errors and warnings in code it should be a clang plugin and you can just change your project clang flags to load the plugin.
In your case it seems like you are using libtooling that alters the source code, you can simply add your script to your project build process prior to the "Compile Sources" stage.
In Xcode -> Click on Project -> click on your desired Target -> Build Phases -> Click on Editor (Menu Bar) -> Add Build Phase -> Add Run Script
Drag the new Run Script to be before Compile Sources
Edit the run script phase to do what you want

Resources