Unity PostProcess Build script fails when running from command line - ios

I am trying to build my project from the command line targeting iOS.
When I build this from the editor, everything works! I have a PostProcess build script that I use.
When I try to build form the command line using the following command:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod BuildScript.BuildIOS -nographics -buildTarget iOS
I am getting an error from my PostProcess build script saying:
Assets/Editor/MyBuildPostprocessor.cs(9,23): error CS0234: The type or namespace name Xcode' does not exist in the namespaceUnityEditor.iOS'. Are you missing an assembly reference?
The line that generates the error is a simple using clause:
using UnityEditor.iOS.Xcode;
So it seems that for some reason, the command line build mechanism does not recognise the UnityEditor.iOS.Xcode assembly, and my PostProcess build script cannot run.
Any ideas?

This is a known case when iOS project is built manually in Unity without errors and fails building on build-server (Unity cloud, Jenkins, TeamCity and others). I think that your case is similar with it. Because if you take a look at the build-server process of building Unity app you'll see that it also uses command line command
/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod BuildScript.BuildIOS -nographics -buildTarget iOS
As for build-server issue, the solution is to put all your post-process scripts inside #if UNITY_IOS condition. And it's possible that you've misunderstood the error you get. Unity says that it doesn't understand XCode namespace while trying to include your post-process script inside your build. And of course, Unity can't include XCode namespace because it's available in Unity Editor only.
So try adding #if UNITY_IOS at the very beginning of your post-process source, even before using section and #endif the the very end of the source. In this case Unity won't compile post-process script during building phase, but will call your post-process script after XCode project will be created.
Unfortunately, I couldn't find any information why manual and command line build processes differ.

Related

Intellij generated Project for Kotlin Multiplatform always show build failed

I am using intellij to create a Kotlin Multiplatform Project for Android and iOS. I have not done any changes to the generated template but when opening the iOS Project in xcode and running it, it shows Build Failed with this logs.
/Users/archie.quinones/Library/Developer/Xcode/DerivedData/iosApp-bwuvtubwlwgrwofszlveznlcosbj/Build/Intermediates.noindex/iosApp.build/Debug-iphonesimulator/app.build/Script-F861D812207FA4320085E80D.sh: line 2: /Users/archie.quinones/IdeaProjects/MppApplication/iosApp/../gradlew: No such file or directory
Command PhaseScriptExecution failed with a nonzero exit code
Can some guide me how to make this work?
Thank you very much.
There is a small confusion here. AFAIK, there should be a note in your Gradle script, with something like
//Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
So please, open the project directory in Terminal and execute gradle wrapper task manually.
This is a known issue of the Kotlin/MPP wizard, here is the ticket. It is not a problem, as you can produce the wrapper by yourself, but I agree, that it might be unobvious.

Using xbuild on iOS application does not generate ipa

We have implemented Continuous Integration with Jenkins to help perform our builds for faster deployment. We have reviewed the documentation provided through the Xamarin website, and have come to an issue. In the walkthrough it describes how to use various plugins to perform the process, we use build scripts to perform our builds because we have some post-actions that we execute after the build, such as submitting the ipa to the TestFlight. The problem is that for iOS it is has been documented that we need to use xbuild instead of mdtool when building from the terminal/command line.
We followed this portion of the guide to construct our xbuild within the build script. It appears that the xbuild command works and creates the project reference dlls and even generates an exe, but does not generate an ipa file even if we set this /p:BuildIpa=true on the xbuild to ensure the ipa generation. We also have provided a location for the ipa to be generated within to ensure we are not missing the file with this /p:IpaPackageDir=$BUILD_PATH. Here is the whole xbuild command that we are currently using, is there something that is incorrect or we are missing?
/Library/Frameworks/Mono.framework/Commands/xbuild $PROJECT_FILE / t:Build /p:BuildIpa=true /p:IpaPackageDir=$BUILD_PATH /p:Configuration=”Release” /p:Platform=”iPhone” /p:OutputPath=$BUILD_PATH
($BUILD_PATH) -- The build path is just “%WORKSPACE%\bin\iPhone\Release\”
I did find this resource as well, which states that xbuild requires the solution file for iOS to build correctly, is that correct?
If this is correct, how do we resolve these errors because I have not been able to find any documentation that explains how to properly define the solution and project files for xbuild deployment, or even how to exclude particular projects during the build:
/Users/developer/.jenkins/workspace/Mobile3_Branch_3.20/ServicePro/iOS/Mobile3.Forms.UWP/Mobile3.Forms.UWP.csproj: warning : Could not find project file /Library/Frameworks/Mono.framework/External/xbuild/Microsoft/WindowsXaml/v14.0/Microsoft.Windows.UI.Xaml.CSharp.targets, to import. Ignoring.
/Users/developer/.jenkins/workspace/Mobile3_Branch_3.20/ServicePro/iOS/Mobile3.ServicePro.UWP/Mobile3.ServicePro.UWP.csproj: warning : Could not find project file /Library/Frameworks/Mono.framework/External/xbuild/Microsoft/WindowsXaml/v14.0/Microsoft.Windows.UI.Xaml.CSharp.targets, to import. Ignoring.
Project "/Users/developer/.jenkins/workspace/Mobile3_Branch_3.20/ServicePro/iOS/Mobile3.sln" (Mobile3.ServicePro.iOS target(s)):
Target ValidateSolutionConfiguration:
/Users/developer/.jenkins/workspace/Mobile3_Branch_3.20/ServicePro/iOS/Mobile3.sln: error : Invalid solution configuration and platform: "“Release”|“iPhone”".
Task "Error" execution -- FAILED
Done building target "ValidateSolutionConfiguration" in project "/Users/developer/.jenkins/workspace/Mobile3_Branch_3.20/ServicePro/iOS/Mobile3.sln".-- FAILED
Done building project "/Users/developer/.jenkins/workspace/Mobile3_Branch_3.20/ServicePro/iOS/Mobile3.sln".-- FAILED
Here is the xbuild command we used to build the solution:
/Library/Frameworks/Mono.framework/Commands/xbuild /p:Configuration=”Release” /p:Platform=”iPhone” /p:OutputPath=$BUILD_PATH /p:BuildIpa=true /p:IpaPackageDir=$BUILD_PATH /t:Build $SOLUTION_FILE
We are not concerned with building the UWP projects when building the iOS project because we have a separate build script that generates the appxbundle file correctly.
You can build an "individual project/app" by setting the SolutionDir and building a .csproj, all the referenced projects will be built and an .ipa created, something like:
xbuild
/p:SolutionDir="./src"
/p:OutputPath="$PWD/artifacts/"
/p:IpaPackageDir="$PWD/artifacts/"
/p:Configuration=Release
/p:Platform=iPhone
/target:Build
src/iOS/XamarinForms.iOS.csproj
Note: This assumes your provisioning profiles are setup on this Mac.

Running clang static checker at project level (iOS app) rather than file by file (xcodebuild)

After solving this Omitted code blocks from clang AST for ObjectiveC, I've tried it on a small Objective C .m file along with an appropriate compile_commands.json and it works properly and I get the entire syntax tree.
Now I'm trying to see if it's possible to run it on the entire xcodebuild
[
{
"directory" : "/Users/xx/Desktop/iOSApplication",
"command" : "xcodebuild clean build CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO -project /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj/",
"file" : "iOSApplication.xcodeproj"
}
]
When I tried to then run clang-check -ast-dump /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj it gives me the errors error: unknown argument: '-project' and error: unable to handle compilation, expected exactly one compiler job in ''
Is it actually possible to run the AST based checker on the entire xcodeproject? Or how should I go about compiling the files 1 at a time?
I've managed to generate the compile_commands.json by following this guide here http://docs.oclint.org/en/stable/guide/xcodebuild.html
However, I'd still like to be able to run my RecursiveASTVisitor on the entire projects. Or alternatively, pass in the xcode project and enumerate all the source files would probably work too.
Anyone has ideas how to go about passing entire xcodebuild project as parameter for RecursiveASTVisitor?
Right now I'm running my ASTVisitor like this ./MyASTChecker ~/Desktop/directory/sample1.m but I'd like to make it do something like ./MyASTChecker ~/Desktop/directory/sampleproject.xcodeproj
The way I do it is at compile time using scan-build. This works for me with cmake/make based projects.
scan-build --use-analyzer=clang -enable-checker <checker_name> make
This will read the makefile and build everything in there while running the specified checker on each file as it's compiled. If you only want to build with some flags or a specific target, you can do this:
scan-build --use-analyzer=clang -enable-checker <checker_name> make <build_options>
If you instead have a cmake based project. You might first want to generate a makefile in a build directory. I do this for that:
cmake <path_to_cmakelists> -DCMAKE_CXX_COMPILER=c++-analyzer -DCMAKE_CC_COMPILER=ccc-analyzer
This followed by scan-build from above will compile and run checker on the source files while each file is being compiled.
I have only tried this with CMAKE / MAKE but should work with xcode like this:
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild
and with build options
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild <build_options>
You can read more about scan-build here

Unity3D/iOS: Running a shell script post-Xcode build

When clicking "Build and Run" for an iOS project, Unity generates an Xcode project, fires up Xcode, builds the project, and runs it on the device. I'd like to run a shell script after Xcode finishes building but before it runs. If this were a mere Xcode project, I could simply add a "Run Script" entry to the Build Phases tab, but since this project is auto-generated by Unity I'm not sure how to proceed.
(I'm running OS X Yosemite, if it matters.)
Depends on how much work you want to put into this.
Solution A: Using Unity's PostProcess
You could use Unity's PostProcess, to modify the Xcode project accordingly.
Just mark a static method with the [PostProcessBuild] annotation, and Unity will execute it after the Unity build.
Example:
[PostProcessBuild]
static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
// modify the Xcode project here, or run the shell script directly (if it is ok to do this already here)
}
Sources:
Source: http://docs.unity3d.com/412/Documentation/ScriptReference/PostProcessBuildAttribute.html
Python script to modify Xcode project (not sure if it can add a shell script in build phases): https://github.com/kronenthaler/mod-pbxproj
How to start a process in C#: How do I start a process from C#?
Solution B: Append the Xcode Project
You could modify your Xcode Project accordingly, and afterwards just use Append when starting the next build and Unity asks what to do, when detecting that the folder already exists.
Solution C: Do it manually
Use Build instead of Build And Run
Modify the Xcode project after Unity finished it's build
Manually Run on the Device using Xcode
Solution D: Run the build in batchmode / use CI like Jenkins
You can invoke the build from the command line (terminal), and do what ever you want during/between the different build steps. But as this is a lot of work, I'd recommend to take a look at a CI like Jenkins. It comes with an installer for Mac OS X and is not that hard to set up. I guess there is a lot of documentation and Q&As about it.
Jenkins: http://jenkins-ci.org
I hope there's something that fits your needs. Just let me know if you need some more help or information. Cheers.

Can't compile plcrashreporter in Xcode 4

I can use the prebuilt framework provided on the plcrashreporter project page when compiling for the device, but not for the simulator. I have the same problem described here.
I assume the prebuilt framework does not support the simulator's architecture, so I downloaded out the plcrashreporter source. I opened the Xcode project and selected the CrashReporter-iOS-Simulator > iPhone 4.3 Simulator target. When I try to build the project, I get this error:
libtool: unknown option character `D' in: -D__IPHONE_OS_VERSION_MIN_REQUIRED=30000
I get the same error when I try to build most of the other targets (such as for device).
My next step was to try adding the source files to my project. I no longer have the aforementioned problem; however, I get this error when I try to compile:
fatal error: 'crash_report.pb-c.h' file not found [2]
#import "crash_report.pb-c.h"
^
1 error generated.
Command clang failed with exit code 1
The crash_report.pb-c.h file which is mentioned in the error message simply does not exist; I've searched the plcrashreporter source tree and the internet. Therefore, I have to assume that this file is supposed to be generated somehow, but I cannot figure out how.
(Commenting out the line in PLCrashReport.m on which crash_report.pb-c.h is included results in numerous other compilation errors.)
You are correct in that the file does not exist normally, nor does crash_report.pb-c.c exist, which will be your next error after this one.
The crash_report.pb.h and crash_report.pb.c files are generated at compile time through a build rule. You need to add a custom script to your build process to make them.
First, make sure you have protoc-c in the plcrashreporter folder of your project (plcrashreporter-1.0/Dependencies/protobuf-2.0.3/bin/protoc-c). They buried it deep. This is what your script will be running.
Then find your crash_report.proto file. This is the main input that protoc-c will be using to create your missing files. You can take this directory and put it manually into your script, OR you can make a rule to run the script on every *.proto file. I do the latter.
Then edit your build rules to include a script that runs protoc-c with the flag --c_out="${DERIVED_FILES_DIR}" and your crash_report.proto file as two inputs, this will output crash_report.pb-c.h and crash_report.pb-c.c into the same directory as where your crash_report.proto file is, which should already be accessible in your project.
The build rules in Xcode 4 (and above) are under your project's target's build rules tab. You add a build rule before all your other build rules. Here's what mine looks like in Xcode:
You'll probably have to fiddle with the directory

Resources