-1

I have a private dependent pod project which is built by us via cocoapods, now I want to use a macro, e.g. ADHOC, to disable some source codes in pod project like this,

#if ADHOC
    // for ad hoc build.
#else 
    // for app store build.
#endif

There are two high-level projects which rely on the pod project and they have different requirements, one for ad-hoc build (projectA), another for app store build (projectB).

After building the dependence relationship in their Podfile, I tried to define ADHOC macro in projectA before importing the entry header file, it just won't build the source code within ADHOC statement, even if importing the entry header file in main project's prefix header file.

I know the MAS_SHORTHAND macro usage in Masonry, it defines two pieces of implementation of those methods, when compiling to the main project, it pre-compiles the shortcut method to full version.

For my case, I expect the macro definition from the main project could affect the pod project. It seems something ridiculous, I know the compile order of the module. It may be impossible to focus on main project only, so I guess the cocoapods may make it works, to inject the macro to pod project before compiling it.

If there are some configuration values for Podfile (not the .podspec) file, please let me know, thanks!

Itachi
  • 5,777
  • 2
  • 37
  • 69

2 Answers2

2

Did it! I built the post_install hook in Podfile to define a custom preprocessor macro, here is it.

# Inject the target macro.
# http://stackoverflow.com/a/27138078/1677041
post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name == "foo_target_name"
            target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'ADHOC=1'
            end
            puts "\n\e[3m\e[32mInject a macro ADHOC to target!\e[0m\e[23m\n\n"
        end
    end
end
Itachi
  • 5,777
  • 2
  • 37
  • 69
0

What you are trying is not a good idea and maybe near impossible.

Pod-project is a dependency of project-A and project-B, this means pod-project is built before building project-A and project-B.

Your proposal will reverse the dependency, having pod-project being dependent on project-A and/or project-B.


The normal way to get around this issue is have pod-project build with both the ad-hoc and app store behaviors (with differently named classes or methods). This is when you use an ADHOC macro in project-A to select the ad-hoc behavior instead of the app store behavior.

#if defined(ADHOC)
#define MyClass MyClassAdHoc
#else
#define MyClass MyClassAppStore
#endif

…

MyClass *instance = [[MyClass] alloc] init];
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • The following code defines two class symbols in pod library, my expected solution is to split the logic code, they are different. I work the expected solution out just now, please check out [my answer](http://stackoverflow.com/a/41303026/1677041). Thanks for your reply! :) – Itachi Dec 23 '16 at 14:23
  • @Itachi encapsulating split logic by subclassing is considered a good idea. Good luck with your solution. – Jeffery Thomas Dec 23 '16 at 14:38
  • Just get what you mean, subclassing. Thanks! – Itachi Dec 23 '16 at 15:04