Xcode: stop lines of code / functions from compiling - swift

I have common files in several projects and I need to stop from compiling some lines and functions for project A, meanwhile it should be compiled for project B.
I know that I can use preprocessor. But it's not convenient for me. Is there any way to stop lines of code from compiling with condition like below?
#if PhotosModuleSettings.type == .documents
... do not commpile
#endif

What's not convenient about using the preprocessor? You can specify the preprocessor macros in build settings of each target, or you can use .xcconfig files to specify them.
There's another simple way to do it, however. Separate the lines and functions that you want to conditionally compile into separate files. Maybe by using Swift extensions or subclassing or just separate global functions, etc..whatever. Then just choose which target(s) and/or project(s) you want those files added as membership.
Depending on your desire to refactor your code to make such a file separation, the preprocessor macros may be the better way to go, though.

You will need to make use of pre processor macros.
Add a configuration for your project, and use that in the pre processor macros.
You can set the value for these configuration in the pre processor macros section for your targets based on your build configuration.
Here is a detailed blog related to the same concept

Related

How can I use #define to load tasks?

I try to find the best way to load addins and tasks in case of breaking changes.
Background
I have created an addin and several tasks based on Cake 0.25.0. The addin as well as the tasks are packaged within a NuGet package. Now I would like to update the used Cake version to 0.32.1 but that is not so easy as I try to explain below.
These NuGet packages that I created are used in multiple repositories and I provide from time to time hotfixes. To be able to automatically load the latest version of the addin/tasks there is no version defined in the pre-processor directive. Means like this:
#load nuget:My.Tasks
#addin nuget:My.AddIn
My Dilemma
I need to make sure that older releases can still use the preprocessor directives as mentioned above. Now I am searching for an approach to achieve this target.
I thought renaming the original package id's would help and everyone who would like to use the upgraded version of my packages have to change the preprocessor directive in their build.cake.
But I am quite sure there must be a smarter way and I hope you can help me.
Next idea I had is to use #define preprocessor directives. So I created a new NuGet package containing only one Cake script named bootstrapper.cake as follows:
#if (V20)
#load nuget:My.Tasks
#addin nuget:My.AddIn
#else
#load nuget:My.Tasks&version=1.0.55
#addin nuget:My.AddIn&version=1.0.55
#end
This does not work as expected. Unfortunately both versions of the respective packages are loaded into the addins respectively tools directory and I am getting errors like "error CS0111: Type 'Submission#0' already defines a member called" and others.
Is there a way to achieve my goal? The idea to use #define was quite nice from my perspective. Maybe it is somehow possible or does anyone know a better way?
Personally I think the best would be, if Cakebuild would support wildcards in the preprocessor directives like
#load nuget:My.Tasks&version=1.0.*
Best Regards
Mr. T
Ifdef won't currently work with preprocessor directives, as they're handled by the C# compiler after preprocessor directives have been executed.
What you can do is to use an environment variable, preprocessor directives support environment variables substitution.
As an example:
Setting variable
RECIPE_VERSION="&version=0.3.0-unstable0400"
Could be used like this
#load nuget:?package=Cake.Receipe%RECIPE_VERSION%
Without the environment variable set, it'll omit the version.

Pre-processor macros in xcconfig files

is is possible to use macros in config files? I want to achieve something like:
if iPad
set variable to 1
else
set variable to 0
Is that possible? I would rather not use scripts for this.
You generally should check this at runtime rather than compile time. See iOS - conditional compilation (xcode).
If you don't do it that way, I typically recommend using different targets as hinted at by #Robert Vojta.
That said, I can imagine cases where this would be useful in some piece of shared code. So...
There is an xcconfig variable you can use called TARGETED_DEVICE_FAMILY. It returns 1 for iPhone and iPod Touch, and 2 for iPad. This can be used to create a kind of macro. I don't highly recommend this approach, but here's how you do it. Let's say you were trying to set some value called SETTINGS:
// Family 1 is iPhone/iPod Touch. Family 2 is iPad
SettingsForFamily1 = ...
SettingsForFamily2 = ...
SETTINGS = $(SettingsForFamily$(TARGETED_DEVICE_FAMILY))
I've done this a few times in my projects (for other problems, not for iPad detection). Every time I've done it, a little more thought has allowed me to remove it and do it a simpler way (usually finding another way to structure my project to remove the need). But this is a technique for creating conditionals in xcconfig.
AFAIK it's not possible. But if you want to solve simple task - lot of common settings and just few variables have different values, you can do this:
generic.xcconfig:
settings for both configs
ipad.xcconfig:
#include "generic.xcconfig"
ipad-specific-settings
iphone.xcconfig
#include "generic.xcconfig"
iphone-specific-settings
This can solve your condition need. I do use this schema frequently.
That's not possible. Configuration files are not preprocessed (and compiled). What are you trying to do?

Minimal references to PRISM/MVVM to have the support for commands

I am looking for information to which PRIMS/MVVM ddls I have to reference to in my project to have available Prism/MVVM functionality for handling Commands. I plan to use only this part of the frameworks.
Regards,
Wojtek
If you only want commanding support I would suggest to completely avoid Prism and that way you will reduce the size of your .xap file.
I would suggest you to look at the code (either in Reflector or open Prism's source) and copy to your project all the files in "Microsoft.Practices.Composite.Presentation.Commands" namespace in the "Microsoft.Practices.Composite.Presentation" assembly. You can ignore CompositeCommand.
If you prefer to reference the assembly, go with "Microsoft.Practices.Composite.Presentation". If you only use commands you won't need other assemblies. The compiler will tell you in case start using other classes from that assembly that depend on another one. The other two dependencies for this assembly (for classes other than commands) are "Microsoft.Practices.Composite" and "Microsot.Practices.ServiceLocation".

How to configure lazy or incremental build in general with Ant?

Java compiler provides incremental build, so javac ant task as well. But most other processes don't.
Considering build processes, they transform some set of files (source) into another set of files (target).
I can distinct two cases here:
Transformator cannot take a subset of source files, only the whole set. Here we can only make lazy build - if no files from source was modified - we skip processing.
Transformator can take a subset of sources files and produce a partial result - incremental build.
What are ant internal, third-party extensions or other tools to implement lazy and incremental build?
Can you provide some widespread buildfile examples?
I am interested this to work with GWT compiler in particular.
The uptodate task is Ant's generic solution to this problem. It's flexible enough to work in most situations where lazy or incremental compilation is desirable.
I had the same problem as you: I have a GWT module as part of my code, and I don't want to pay the (hefty!) cost of recompiling it when I don't need to. The solution in my case looked something like this:
<uptodate property="gwtCompile.mymodule.notRequired"
targetfile="www/com.example.MyGwtModule/com.example.MyGwtModule.nocache.js">
<srcfiles dir="src" includes="**"/>
</uptodate>
<target name="compile-mymodule-gwt" unless="gwtCompile.mymodule.notRequired">
<compile-gwt-module module="com.example.MyGwtModule"/>
</target>
Related to GWT, it's not possible to do incremental builds because the GWT compiler looks at all the source code at once and optimizes and inlines code. This means code that wasn't changed could be evaluated differently, for example if you start using a method from a class that wasn't changed, the method was in the previous compilation step left out, but now needs to be compiled in.

MS VS-2005 Compiler optimization not removing unused/unexecuted code

I have a workspace built using MS-Visual Studio 2005 with all C code.In that i see many functions which are not called but they are still compiled(they are not under any compile time macro to disable them from compiling).
I set following optimization settings for the MS-VS2005 project to remove that unused code:-
Optimization level - /Ox
Enable whole program optimization - /GL
I tried both Favor speed /Ot and Favor Size /Os
Inspite of all these options, when i see the linker generated map file, I see the symbols(unsed functions) names present in the map file.
Am I missing something? I want to completely remove the unused code.
How do I do this?
The compiler compiles C files one-at-a-time. Therefore, while compiling a C-file that does contains an unused function, the compiler cannot be sure that it will not be called from another file and hence it will compile that function too. However, if that function were declared as static (file-scope), then the compiler would know it is not used and hence remove it.
Even with whole program optimization, I think it would still not be done since the compilation could be for a library.
Linkers do something similar to what you are looking for. If your code links against a library containing multiple objects, then any objects that do not contain functions used by your code (directly or indirectly) would not be included in the final executable.
One option would be to separate your code into individual libraries and object files.
PS - This is just my guess. The behavior of the compiler (with whole program optimization) or linker essentially depends on the design choices of that particular compiler or linker
On our projects we have a flag set under the project properties\Linker\Refrences. We set it to Eliminate Unreferenced Data (/OPT:REF), according to the description this is supposed to remove function calls or data that are never used. I am just going by the description, I have never tested this or worked with it. But I just happened to see it within the last hour and figured it might be something you could try.