KISS

Keep It Simple Stupid

Podfile's post_install hook example

| comments

Updated on 2013-08-10: thanks to Esteban Torres; updated the script with a test if there’s settings in build file, and if not create the hash.

You know this truth: frameworks are great and easy when your tasks are completely covered by its functionality, but the moment you need to do something out-of-box there are going to be issues there.

CocoaPods is a great tool to manage the dependencies of iOS/OS X projects, here are my posts about it: installing, usage1, usage2, some thoughts, and notes. It’s been working fine in my project, and when one time I updated the Pods project, it brought the updated version of JSONKit 1.5pre.

That new version generated a bunch of warnings:

1
2
3
warning: values of type 'NSUInteger' should not be used as format arguments; add an explicit cast to 'unsigned long' instead [-Wformat]

warning: direct access to objective-c's isa is deprecated in favor of object_setClass() and object_getClass() [-Wdeprecated-objc-isa-usage]

This is a known issue, and the JSONKit’s podspec file used to set certain compiler warnings to suppress the warnings, which was later removed: https://github.com/CocoaPods/Specs/commit/017c423e46e58049b7f68b2ac9d901533d515132#diff-6. One option was to use the inhibit_all_warnings! flag (http://docs.cocoapods.org/podfile.html#inhibit_all_warnings_bang), but it would disable ALL warnings (not very good).

So a more suitable solution is to apply the flags on my side. Podfile provides two hooks: pre_install and post_install for that purpose. I couldn’t find any serious examples of using them, so I had to figure it out myself (also note that I have almost no knowledge of Ruby). Here’s my Podfile with a post_install hook example:

(Podfile) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
platform :ios

pod 'JSONKit', '1.5pre'

# The post install hook add certain compiler flags for JSONKit files so that
# they won't generate warnings. This had been done in the podspec before, but
# was removed later for some reason.
post_install do |installer|
    # Adds the specified compiler flags to the given file in the project.
    #
    # @param [Xcodeproj::Project] project
    #                             The Xcode project instance.
    #
    # @param [String] filename
    #                 The name of the file to work with.
    #
    # @param [String] new_compiler_flags
    #                 The compiler flags to add.
    #
    # @example Disable some warning switches for JSONKit:
    #   add_compiler_flags(installer.project,
    #       "JSONKit.m",
    #       "-Wno-deprecated-objc-isa-usage -Wno-format")
    #
    def add_compiler_flags(project, filename, new_compiler_flags)
        # find all PBXFileReference objects of the given file
        files = project.files().select { |file|
            file.display_name() == filename
        }

        # get the PBXBuildFile references of the found files
        # PBXBuildFile actually contains flags for building the file
        build_files = files.map { |file|
            file.build_files()
        }.compact.flatten

        # compiler flags key in settings
        compiler_flags_key = "COMPILER_FLAGS"

        if build_files.length > 0
            build_files.each { |build_file|
                settings = build_file.settings
                if settings.nil?
                    # If we don't have settings for the file we create a new hash
                    settings = Hash[compiler_flags_key, new_compiler_flags]
                else
                    compiler_flags = settings[compiler_flags_key]
                    compiler_flags = (compiler_flags.nil?) ?
                        new_compiler_flags :
                        (compiler_flags + " " + new_compiler_flags)
                    settings[compiler_flags_key] = compiler_flags
                end
                build_file.settings = settings
            }
        else
            puts "No build file refs found for #{filename}!"
        end
    end

    # compiler flags that turn off the JSONKit's warnings
    JSONKIT_FLAGS = "-Wno-deprecated-objc-isa-usage -Wno-format"
    add_compiler_flags(installer.project, "JSONKit.m", JSONKIT_FLAGS)
end

It adds the -Wno-deprecated-objc-isa-usage -Wno-format compiler flags to JSONKit.m file. Leave a comment if you’ve found this post useful (for any reason) :)

dev, iOS

Don't hesitate to leave a comment below. NB! If you don't see a comment form under the post, it's most likely that an extension (such as Ghostery, NoScript, or AdBlock) of your browser blocks the scripts from disqus.com, and you can unblock that.

« fixtags.py and gPodder 3 Yes. It runs with NetWare »

Comments