cocos2d-x-3rd-party-libs-src (v3-deps-109) を bitcode 有効にしてビルド

# 環境

1. Mac OS X 10.11.6 (英語)
2. Xcode 7.3.1

# 手順

1. [cocos2d-x-3rd-party-libs-src](https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src) を git clone
 
		$ git clone https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src.git
		Cloning into 'cocos2d-x-3rd-party-libs-src'...
		remote: Counting objects: 2324, done.
		remote: Total 2324 (delta 0), reused 0 (delta 0), pack-reused 2324
		Receiving objects: 100% (2324/2324), 434.28 KiB | 392.00 KiB/s, done.
		Resolving deltas: 100% (1252/1252), done.
		Checking connectivity... done.

2. cocos2d-x-3.13.1 用に git checkout
    1. cocos2d-x-3.13.1 の [v3-deps-109 リリース](https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/releases/tag/v3-deps-109) (2016-08-16) は、日付からコミット 0d97039 (2016-08-11) であると仮定

			$ cd cocos2d-x-3rd-party-libs-src
			$ git checkout 0d97039
			Note: checking out '0d97039'.

			You are in 'detached HEAD' state. You can look around, make experimental
			changes and commit them, and you can discard any commits you make in this
			state without impacting any branches by performing another checkout.

			If you want to create a new branch to retain commits you create, you may
			do so (now or later) by using -b with the checkout command again. Example:

			  git checkout -b <new-branch-name>

			HEAD is now at 0d97039... Merge pull request #109 from Watson1978/v3

3. README.md に従ってビルド用のツールをインストール

		$ brew update
		$ brew install git
		$ brew install cmake
		$ brew install autoconf
		$ brew install automake
		$ brew install libtool
		$ brew list --versions
		autoconf 2.69
		automake 1.15
		cmake 3.6.1
		git 2.9.2
		libtool 2.4.6_1

4. README.md に従って bitcode を有効に

    1. build/ios.ini の

            cfg_build_bitcode=
         を

            cfg_build_bitcode="-fembed-bitcode"
         に変更

4. 以下のチェックサムエラーを回避

            curl-7.50.0.tar.gz: FAILED
            shasum: WARNING: 1 computed checksum did NOT match

    1. contrib/src/curl/SHA512SUMS を https://curl.haxx.se/download.html からダウンロードできる tar.gz のチェックサムに変更（ v3-deps-109 は 7.50.0-DEV を使ってる）

			$ more contrib/src/curl/SHA512SUMS 
			bf3ca3197fc7bfe365c624a66bfd40904947aa47e7520b9072512d28a7b1f1fd90271ab7ea01b725917eb9014c8b5d5bf4727700cb336798bb14c941b9fe97e6  curl-7.50.0.tar.gz
			$ echo "a4d0c8f3591d9d88ca404244256eb479350de45232747eb8396421c290fdc637c7f873b1ef3f299d7f4d443a01d4be7c4fbf468b1b79a8cdcef0da9e85949c42  curl-7.50.0.tar.gz" > contrib/src/curl/SHA512SUMS 


5. iOS 用に release ビルド

		$ cd build
		$ ./build.sh -p=ios --libs=chipmunk,curl,freetype,jpeg,openssl,png,tiff,webp,websockets --arch=all --mode=release

  1. ```--libs=all``` だと rapidjson でエラーが起きる

			create folder for library with specify arch. ios/rapidjson/prebuilt/x86_64
			cp: .../cocos2d-x-3rd-party-libs-src/build/../contrib/install-ios/x86_64/lib/librapidjson.a: No such file or directory
			Copying needed heder files
			cleaning up
			yes
			create fat library librapidjson for 
			error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: no input files specified
			fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo [input_file] ... [-arch <arch_type> input_file] ... [-info] [-detailed_info] [-output output_file] [-create] [-arch_blank <arch_type>] [-thin <arch_type>] [-remove <arch_type>] ... [-extract <arch_type>] ... [-extract_family <arch_type>] ... [-verify_arch <arch_type> ...] [-replace <arch_type> <file_name>] ...
			fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: ios/rapidjson/prebuilt/librapidjson.a (No such file or directory)

      1. そもそも cocos2d-x-3.13.1/external に rapidjson は存在しない

    1. コミット 0d97039 の contrib/src に存在しているのに cocos2d-x-3.13.1/external に存在しないもののリスト
        1. angle
        2. glew
        3. iconv
        4. ogg
        5. openssl
        6. rapidjson
    2. 逆に cocos2d-x-3.12/external に存在して contrib/src に存在しないもののリスト
        1. Box2D
        2. ConvertUTF
        3. bullet
        4. clipper
        5. edtaa3func
        6. flatbuffers
        7. json
        8. poly2tri
        9. recast
        10. spidermonkey (prebuilt/ios/libjs_static.a も存在する)
        11. tinyxml2
        12. unzip
        13. xxhash
        14. xxtea
        15. (プラットフォーム固有の *-specific は除く）
    3. 両者に共通して存在するもののリスト
        1. chipmunk
        2. curl
        3. freetype (freetype2 in external)
        4. glfw (glfw3 in external)
        5. jpeg
        6. lua (lua/lua in external)
        7. luajit (lua/luajit in external)
        7. png
        8. sqlite (sqlite3 in external)
        9. tiff
        10. webp
        11. websockets
        12. zlib
    4. 両者に共通するもの全てを ```--libs``` に指定すると、以下のエラーが出る

    		Invalid library names! Only png zlib lua luajit websockets curl freetype jpeg tiff webp chipmunk openssl rapidjson is acceptable!

    5. 従って glfw と sqlite はビルドしない
    6. また cocos2d-x-3.12/external/curl/prebuilt/ios に libssl.a が存在するので openssl もビルドする
        1. 同様に curl に含まれている libcrypto.a もビルド指定するべきだと思うが、勝手にビルドされてた
    7. external/zlib/prebuilt に ios ディレクトリが存在しないので zlib もビルドしない
    8. lua は以下のエラーが出るのでビルドから外す

			Error: pointer size mismatch in cross-build.
			Try: make HOST_CC="gcc -m32" CROSS=...

    9. luajit は以下のエラーが出るのでビルドから外す

			ld: -undefined and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together

  1. build の最初の方で以下の警告が出るが

			SDKROOT not specified, assuming /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk

     1. SDKROOT を設定すると以下のエラーが出てしまった

				CMake Error at /usr/local/Cellar/cmake/3.6.1/share/cmake/Modules/CMakeTestCCompiler.cmake:61 (message):
				  The C compiler "/usr/bin/xcrun" is not able to compile a simple test
				  program.



# ビルド結果を cocos2d-x-3.13.1/external へ配置

	$ cd cocos2d-x/external

## chipmunk

	$ cd chipmunk/prebuilt/ios

	$ diff -r ../../include ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/chipmunk/include
	diff -r ../../include/chipmunk/chipmunk.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/chipmunk/include/chipmunk/chipmunk.h
	31,35c31
	<     #if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
	<         #define CP_EXPORT __declspec(dllexport)
	<     #else
	< 	    #define CP_EXPORT
	<     #endif
	---
	> 	#define CP_EXPORT __declspec(dllexport)

	$ mv libchipmunk.a libchipmunk.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/chipmunk/prebuilt/libchipmunk.a

	$ cd ../../..

1. diff で差異が見つかった
    1. external のバージョンは [cocos2d-x-3rd-party-libs-bin 固有のコード](https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/commit/120b93f56b11d12cd610fa6ba018895bfedd6527)で、[本家](https://github.com/slembcke/Chipmunk2D/blob/master/include/chipmunk/chipmunk.h)には存在しないし [chipmunk/cocos2d.patch](https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src/blob/v3/contrib/src/chipmunk/cocos2d.patch) にも存在しない
        1. cocos2d-x-3rd-party-libs-bin は cocos2d-x-3rd-party-libs-src からビルドしているものではないのかも・・・
        2. ```#ifdef WIN32``` で囲まれた部分での差異なのでコンパイル結果には影響ないはず
            1. この差異はとりあえず無視

## curl (with crypto and ssl)

	$ cd curl/prebuilt/ios

	$ diff -rq ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/curl/include
	Only in ../../include/ios/curl: curlbuild-32.h
	Only in ../../include/ios/curl: curlbuild-64.h
	Files ../../include/ios/curl/curlbuild.h and ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/curl/include/curl/curlbuild.h differ
	Files ../../include/ios/curl/curlver.h and ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/curl/include/curl/curlver.h differ
	$ diff ../../include/ios/curl/curlbuild-64.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/curl/include/curl/curlbuild.h
	$ diff ../../include/ios/curl/curlver.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/curl/include/curl/curlver.h
	33c33
	< #define LIBCURL_VERSION "7.50.0-DEV"
	---
	> #define LIBCURL_VERSION "7.50.0"
	71c71
	< #define LIBCURL_TIMESTAMP "DEV"
	---
	> #define LIBCURL_TIMESTAMP "Thu Jul 21 08:55:43 UTC 2016"

	$ mv libcurl.a libcurl.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/curl/prebuilt/libcurl.a

	$ mv libcrypto.a libcrypto.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/crypto/prebuilt/libcrypto.a

	$ mv libssl.a libssl.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/ssl/prebuilt/libssl.a

	$ cd ../../..

1. [external の curlbuild.h](https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/blob/v3/curl/include/ios/curl/curlbuild.h) は ```__LP64__``` が定義されてると curlbuild-64.h を読み込み、それが cocos2d-x-3rd-party-libs-src の curlbuild.h と同一
    1. この差異もとりあえず無視

## freetype

	$ cd freetype2/prebuilt/ios

	$ diff -r ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/freetype/include

	$ mv libfreetype.a libfreetype.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/freetype/prebuilt/libfreetype.a

	$ cd ../../..

1. ようやく差異がないものが出てきた

## jpeg

	$ cd jpeg/prebuilt/ios

	$ diff -r ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/jpeg/include
	diff -r ../../include/ios/jconfig.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/jpeg/include/jconfig.h
	21c21
	< /* Define "boolean" as unsigned char, not int, on Windows systems. */
	---
	> /* Define "boolean" as unsigned char, not enum, on Windows systems. */
	25a26,31
	> #ifndef FALSE			/* in case these macros already exist */
	> #define FALSE	0		/* values of boolean */
	> #endif
	> #ifndef TRUE
	> #define TRUE	1
	> #endif
	diff -r ../../include/ios/jmorecfg.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/jpeg/include/jmorecfg.h
	5c5
	<  * Modified 1997-2012 by Guido Vollbeding.
	---
	>  * Modified 1997-2013 by Guido Vollbeding.
	17a18,20
	>  *   9   for 9-bit sample values
	>  *   10  for 10-bit sample values
	>  *   11  for 11-bit sample values
	19,21c22,30
	<  * Only 8 and 12 are legal data precisions for lossy JPEG according to the
	<  * JPEG standard, and the IJG code does not support anything else!
	<  * We do not support run-time selection of data precision, sorry.
	---
	>  * Only 8, 9, 10, 11, and 12 bits sample data precision are supported for
	>  * full-feature DCT processing.  Further depths up to 16-bit may be added
	>  * later for the lossless modes of operation.
	>  * Run-time selection and conversion of data precision will be added later
	>  * and are currently not supported, sorry.
	>  * Exception:  The transcoding part (jpegtran) supports all settings in a
	>  * single instance, since it operates on the level of DCT coefficients and
	>  * not sample values.  The DCT coefficients are of the same type (16 bits)
	>  * in all cases (see below).
	24c33
	< #define BITS_IN_JSAMPLE  8	/* use 8 or 12 */
	---
	> #define BITS_IN_JSAMPLE  8	/* use 8, 9, 10, 11, or 12 */
	79a89,130
	> #if BITS_IN_JSAMPLE == 9
	> /* JSAMPLE should be the smallest type that will hold the values 0..511.
	>  * On nearly all machines "short" will do nicely.
	>  */
	> 
	> typedef short JSAMPLE;
	> #define GETJSAMPLE(value)  ((int) (value))
	> 
	> #define MAXJSAMPLE	511
	> #define CENTERJSAMPLE	256
	> 
	> #endif /* BITS_IN_JSAMPLE == 9 */
	> 
	> 
	> #if BITS_IN_JSAMPLE == 10
	> /* JSAMPLE should be the smallest type that will hold the values 0..1023.
	>  * On nearly all machines "short" will do nicely.
	>  */
	> 
	> typedef short JSAMPLE;
	> #define GETJSAMPLE(value)  ((int) (value))
	> 
	> #define MAXJSAMPLE	1023
	> #define CENTERJSAMPLE	512
	> 
	> #endif /* BITS_IN_JSAMPLE == 10 */
	> 
	> 
	> #if BITS_IN_JSAMPLE == 11
	> /* JSAMPLE should be the smallest type that will hold the values 0..2047.
	>  * On nearly all machines "short" will do nicely.
	>  */
	> 
	> typedef short JSAMPLE;
	> #define GETJSAMPLE(value)  ((int) (value))
	> 
	> #define MAXJSAMPLE	2047
	> #define CENTERJSAMPLE	1024
	> 
	> #endif /* BITS_IN_JSAMPLE == 11 */
	> 
	> 
	255c306,309
	< #ifdef HAVE_BOOLEAN
	---
	> #ifndef HAVE_BOOLEAN
	> #if defined FALSE || defined TRUE || defined QGLOBAL_H
	> /* Qt3 defines FALSE and TRUE as "const" variables in qglobal.h */
	> typedef int boolean;
	264a319
	> #endif
	302,306c357,362
	< /* Note: if you selected 12-bit data precision, it is dangerous to turn off
	<  * ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only good for 8-bit
	<  * precision, so jchuff.c normally uses entropy optimization to compute
	<  * usable tables for higher precision.  If you don't want to do optimization,
	<  * you'll have to supply different default Huffman tables.
	---
	> /* Note: if you selected more than 8-bit data precision, it is dangerous to
	>  * turn off ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only
	>  * good for 8-bit precision, so arithmetic coding is recommended for higher
	>  * precision.  The Huffman encoder normally uses entropy optimization to
	>  * compute usable tables for higher precision.  Otherwise, you'll have to
	>  * supply different default Huffman tables.
	317c373
	< #define IDCT_SCALING_SUPPORTED	    /* Output rescaling via IDCT? */
	---
	> #define IDCT_SCALING_SUPPORTED	    /* Output rescaling via IDCT? (Requires DCT_ISLOW)*/
	diff -r ../../include/ios/jpeglib.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/jpeg/include/jpeglib.h
	5c5
	<  * Modified 2002-2012 by Guido Vollbeding.
	---
	>  * Modified 2002-2015 by Guido Vollbeding.
	42c42
	< #define JPEG_LIB_VERSION_MINOR  0
	---
	> #define JPEG_LIB_VERSION_MINOR  2
	46,47c46,47
	<  * All of these are specified by the JPEG standard, so don't change them
	<  * if you want to be compatible.
	---
	>  * All of these are specified by the JPEG standard,
	>  * so don't change them if you want to be compatible.
	160c160,161
	<    * downsampled_width = ceil(image_width * Hi/Hmax * DCT_h_scaled_size/DCTSIZE)
	---
	>    * downsampled_width =
	>    *   ceil(image_width * Hi/Hmax * DCT_h_scaled_size/block_size)
	165,167c166,172
	<   /* This flag is used only for decompression.  In cases where some of the
	<    * components will be ignored (eg grayscale output from YCbCr image),
	<    * we can skip most computations for the unused components.
	---
	>   /* For decompression, in cases where some of the components will be
	>    * ignored (eg grayscale output from YCbCr image), we can skip most
	>    * computations for the unused components.
	>    * For compression, some of the components will need further quantization
	>    * scale by factor of 2 after DCT (eg BG_YCC output from normal RGB input).
	>    * The field is first set TRUE for decompression, FALSE for compression
	>    * in initial_setup, and then adapted in color conversion setup.
	169c174
	<   boolean component_needed;	/* do we need the value of this component? */
	---
	>   boolean component_needed;
	218,219c223,224
	< 	JCS_RGB,		/* red/green/blue */
	< 	JCS_YCbCr,		/* Y/Cb/Cr (also known as YUV) */
	---
	> 	JCS_RGB,		/* red/green/blue, standard RGB (sRGB) */
	> 	JCS_YCbCr,		/* Y/Cb/Cr (also known as YUV), standard YCC */
	221c226,228
	< 	JCS_YCCK		/* Y/Cb/Cr/K */
	---
	> 	JCS_YCCK,		/* Y/Cb/Cr/K */
	> 	JCS_BG_RGB,		/* big gamut red/green/blue, bg-sRGB */
	> 	JCS_BG_YCC		/* big gamut Y/Cb/Cr, bg-sYCC */
	975c982
	< 			      unsigned char * inbuffer,
	---
	> 			      const unsigned char * inbuffer,

	$ mv libjpeg.a libjpeg.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/jpeg/prebuilt/libjpeg.a

	$ cd ../../..

1. 差異があるが、とりあえず無視


## png

	$ cd png/prebuilt/ios

	$ diff -r ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/png/include

	$ mv libpng.a libpng.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/png/prebuilt/libpng.a

	$ cd ../../..

1. これも差異はなさそう

## tiff

	$ cd tiff/prebuilt/ios

	$ diff -rq ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/tiff/include
	Only in ../../include/ios: tiffconf-32.h
	Only in ../../include/ios: tiffconf-64.h
	Files ../../include/ios/tiffconf.h and ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/tiff/include/tiffconf.h differ
	$ diff ../../include/ios/tiffconf-64.h ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/tiff/include/tiffconf.h


	$ mv libtiff.a libtiff.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/tiff/prebuilt/libtiff.a

	$ cd ../../..

1. [external の tiffconf.h](https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/blob/v3/tiff/include/ios/tiffconf.h) は ```__LP64__``` が定義されてると tiffconf-64.h を読み込み、それが cocos2d-x-3rd-party-libs-src の tiffconf.h と同一
    1. この差異もとりあえず無視

## webp

	$ cd webp/prebuilt/ios

	$ diff -r ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/webp/include/webp

	$ mv libwebp.a libwebp.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/webp/prebuilt/libwebp.a

	$ cd ../../..

## websockets

	$ cd websockets/prebuilt/ios

	$ diff -r ../../include/ios ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/websockets/include
	Only in ../../include/ios: lws_config.h

	$ mv libwebsockets.a libwebsockets.a.dist
	$ ln -s ../../../../../cocos2d-x-3rd-party-libs-src/build/ios/websockets/prebuilt/libwebsockets.a

	$ cd ../../..

1. lws_config.h が external にしか存在しないが libwebsockets.h は同じ
    1. この差異もとりあえず無視