build_darwin_framework.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env -S python3 -B
  2. # Copyright (c) 2022 Project Matter Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import glob
  17. import os
  18. import platform
  19. from subprocess import PIPE, Popen
  20. def get_file_from_pigweed(name):
  21. CHIP_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
  22. PIGWEED = os.path.join(CHIP_ROOT, ".environment/cipd/packages/pigweed")
  23. pattern = os.path.join(PIGWEED, '**', name)
  24. for filename in glob.glob(pattern, recursive=True):
  25. if os.path.isfile(filename):
  26. return filename
  27. def run_command(command):
  28. returncode = -1
  29. command_log = b''
  30. print("Running {}".format(command))
  31. with Popen(command, cwd=os.getcwd(), stdout=PIPE, stderr=PIPE) as process:
  32. for line in process.stdout:
  33. command_log += line
  34. for line in process.stderr:
  35. command_log += line
  36. process.wait()
  37. returncode = process.returncode
  38. with open(args.log_path, "wb") as f:
  39. f.write(command_log)
  40. if returncode != 0:
  41. # command_log is binary, so decoding as utf-8 might technically fail. We don't want
  42. # to throw on that.
  43. try:
  44. print("Failure log: {}".format(command_log.decode()))
  45. except Exception:
  46. pass
  47. return returncode
  48. def build_darwin_framework(args):
  49. abs_path = os.path.abspath(args.out_path)
  50. if not os.path.exists(abs_path):
  51. os.mkdir(abs_path)
  52. command = [
  53. 'xcodebuild',
  54. '-scheme',
  55. args.target,
  56. '-sdk',
  57. args.target_sdk,
  58. '-project',
  59. args.project_path,
  60. '-derivedDataPath',
  61. abs_path,
  62. "ARCHS={}".format(args.target_arch),
  63. ]
  64. if args.target_sdk != "macosx":
  65. command += [
  66. # Build Matter.framework as a static library
  67. "SUPPORTS_TEXT_BASED_API=NO",
  68. "MACH_O_TYPE=staticlib",
  69. # Change visibility flags such that both darwin-framework-tool and Matter.framework
  70. # are built with the same flags.
  71. "GCC_INLINES_ARE_PRIVATE_EXTERN=NO",
  72. "GCC_SYMBOLS_PRIVATE_EXTERN=NO",
  73. ]
  74. options = {
  75. 'CHIP_INET_CONFIG_ENABLE_IPV4': args.ipv4,
  76. 'CHIP_IS_ASAN': args.asan,
  77. 'CHIP_IS_BLE': args.ble,
  78. 'CHIP_IS_CLANG': args.clang,
  79. 'CHIP_ENABLE_ENCODING_SENTINEL_ENUM_VALUES': args.enable_encoding_sentinel_enum_values
  80. }
  81. for option in options:
  82. command += ["{}={}".format(option, "YES" if options[option] else "NO")]
  83. defines = 'GCC_PREPROCESSOR_DEFINITIONS=${inherited} MTR_NO_AVAILABILITY=1'
  84. if args.enable_provisional_framework_features:
  85. defines += ' MTR_ENABLE_PROVISIONAL=1'
  86. command += [defines]
  87. cflags = ["${inherited}"]
  88. ldflags = ["${inherited}"]
  89. if args.clang:
  90. command += [
  91. "CC={}".format(get_file_from_pigweed("clang")),
  92. "CXX={}".format(get_file_from_pigweed("clang++")),
  93. "COMPILER_INDEX_STORE_ENABLE=NO",
  94. "CLANG_ENABLE_MODULES=NO",
  95. ]
  96. ldflags += [
  97. "-nostdlib++",
  98. get_file_from_pigweed("libc++.a"),
  99. ]
  100. if args.asan:
  101. flags = ["-fsanitize=address", "-fno-omit-frame-pointer"]
  102. cflags += flags
  103. ldflags += flags
  104. if args.clang:
  105. ldflags += [
  106. get_file_from_pigweed("libclang_rt.asan_osx_dynamic.dylib")
  107. ]
  108. if args.enable_encoding_sentinel_enum_values:
  109. cflags += ["-DCHIP_CONFIG_IM_ENABLE_ENCODING_SENTINEL_ENUM_VALUES=1"]
  110. command += ["OTHER_CFLAGS=" + ' '.join(cflags), "OTHER_LDFLAGS=" + ' '.join(ldflags)]
  111. command_result = run_command(command)
  112. print("Build Framework Result: {}".format(command_result))
  113. exit(command_result)
  114. if __name__ == "__main__":
  115. parser = argparse.ArgumentParser(
  116. description="Build the Matter Darwin framework")
  117. parser.add_argument(
  118. "--project_path",
  119. default="src/darwin/Framework/Matter.xcodeproj",
  120. help="Set the project path",
  121. required=True,
  122. )
  123. parser.add_argument(
  124. "--out_path",
  125. default="/tmp/macos_framework_output",
  126. help="Output lpath for framework",
  127. required=True,
  128. )
  129. parser.add_argument("--target",
  130. default="Matter",
  131. help="Name of target to build",
  132. required=True)
  133. parser.add_argument("--target_sdk",
  134. default="macosx",
  135. help="Set the target sdk",
  136. required=False,
  137. )
  138. parser.add_argument("--target_arch",
  139. default=platform.machine(),
  140. help="Set the target architecture",
  141. required=False,
  142. )
  143. parser.add_argument("--log_path",
  144. help="Output log file destination",
  145. required=True)
  146. parser.add_argument('--ipv4', action=argparse.BooleanOptionalAction)
  147. parser.add_argument('--asan', action=argparse.BooleanOptionalAction)
  148. parser.add_argument('--ble', action=argparse.BooleanOptionalAction)
  149. parser.add_argument('--clang', action=argparse.BooleanOptionalAction)
  150. parser.add_argument('--enable-encoding-sentinel-enum-values', action=argparse.BooleanOptionalAction)
  151. parser.add_argument('--enable-provisional-framework-features', action=argparse.BooleanOptionalAction)
  152. args = parser.parse_args()
  153. build_darwin_framework(args)