How can I "Switch Platform" from a script on Mac?

I have a cross-platform game that I want to build automatically for iOS and Android. My build machine is a Mac Mini, but I would like to be able to build on multiple machines using Jenkins. At this point, I have scripts set up that make the Android build nicely, but there is a lot of interactive preparation that I was hoping to avoid through automation.

The big hangup right now is this: whenever I have a “clean workspace”, the editor seems to try and build “PC, Mac & Linux Standalone”. This leads to compilation errors regarding conditionally-compiled blocks (#if UNITY_IPHONE and so on).

If I manually fire up Unity once in that folder and use the “Switch Platform” button from the Build Settings panel, all subsequent automated builds work just fine.

Is there any way to do the same thing as “Switch Platform” from the command line? I am using something like the AutoBuilder script from http://wiki.unity3d.com/index.php?title=AutoBuilder with “executeMethod” from the command line. The script has a line to switch target, but maybe it is not effective:

EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);

The command line I am using is:

Unity -batchmode -quit -executeMethod Builder.Android -projectPath ./proj -logfile unity_build_log.txt

Do I need to do anything more than that?

I tested my colleague’s theory and it seems to be correct.

In order to use a script in Assets/Editor the game must be able to compile everything for whatever platform is currently selected. This means that if you want to do an automated build from a clean checkout, the game must compile for Standalone.

In my case, since we do not actually care about building for Standalone, this means I can take some liberties with #if UNITY_STANDALONE to just disable sections of code that would otherwise have caused compilation errors. The compilation phase leading up to invocation of the argument passed to -executeMethod is entirely in Editor-space, so UNITY_EDITOR is also defined.

By combining this knowledge and the usage of smcs.rsp as described in the Global Custom Defaults section of Unity - Manual: Conditional Compilation I was able to get my automated build up and running without excess human intervention.