ccmake.bbclass 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. inherit terminal
  7. python do_ccmake() {
  8. import shutil
  9. # copy current config for diffing
  10. config = os.path.join(d.getVar("B"), "CMakeCache.txt")
  11. if os.path.exists(config):
  12. shutil.copy(config, config + ".orig")
  13. oe_terminal(d.expand("ccmake ${OECMAKE_GENERATOR_ARGS} ${OECMAKE_SOURCEPATH} -Wno-dev"),
  14. d.getVar("PN") + " - ccmake", d)
  15. if os.path.exists(config) and os.path.exists(config + ".orig"):
  16. if bb.utils.md5_file(config) != bb.utils.md5_file(config + ".orig"):
  17. # the cmake class uses cmake --build, which will by default
  18. # regenerate configuration, simply mark the compile step as tainted
  19. # to ensure it is re-run
  20. bb.note("Configuration changed, recompile will be forced")
  21. bb.build.write_taint('do_compile', d)
  22. }
  23. do_ccmake[depends] += "cmake-native:do_populate_sysroot"
  24. do_ccmake[nostamp] = "1"
  25. do_ccmake[dirs] = "${B}"
  26. addtask ccmake after do_configure
  27. def cmake_parse_config_cache(path):
  28. with open(path, "r") as f:
  29. for i in f:
  30. i = i.rstrip("\n")
  31. if len(i) == 0 or i.startswith("//") or i.startswith("#"):
  32. continue # empty or comment
  33. key, value = i.split("=", 1)
  34. key, keytype = key.split(":")
  35. if keytype in ["INTERNAL", "STATIC"]:
  36. continue # skip internal and static config options
  37. yield key, keytype, value
  38. def cmake_diff_config_vars(a, b):
  39. removed, added = [], []
  40. for ak, akt, av in a:
  41. found = False
  42. for bk, bkt, bv in b:
  43. if bk == ak:
  44. found = True
  45. if bkt != akt or bv != av: # changed
  46. removed.append((ak, akt, av))
  47. added.append((bk, bkt, bv))
  48. break
  49. # remove any missing from b
  50. if not found:
  51. removed.append((ak, akt, av))
  52. # add any missing from a
  53. for bk, bkt, bv in b:
  54. if not any(bk == ak for ak, akt, av in a):
  55. added.append((bk, bkt, bv))
  56. return removed, added
  57. python do_ccmake_diffconfig() {
  58. import shutil
  59. config = os.path.join(d.getVar("B"), "CMakeCache.txt")
  60. if os.path.exists(config) and os.path.exists(config + ".orig"):
  61. if bb.utils.md5_file(config) != bb.utils.md5_file(config + ".orig"):
  62. # scan the changed options
  63. old = list(cmake_parse_config_cache(config + ".orig"))
  64. new = list(cmake_parse_config_cache(config))
  65. _, added = cmake_diff_config_vars(old, new)
  66. if len(added) != 0:
  67. with open(d.expand("${WORKDIR}/configuration.inc"), "w") as f:
  68. f.write("EXTRA_OECMAKE += \" \\\n")
  69. for k, kt, v in added:
  70. escaped = v if " " not in v else "\"{0}\"".format(v)
  71. f.write(" -D{0}:{1}={2} \\\n".format(k, kt, escaped))
  72. f.write(" \"\n")
  73. bb.plain("Configuration recipe fragment written to: {0}".format(d.expand("${WORKDIR}/configuration.inc")))
  74. with open(d.expand("${WORKDIR}/site-file.cmake"), "w") as f:
  75. for k, kt, v in added:
  76. f.write("SET({0} \"{1}\" CACHE {2} \"\")\n".format(k, v, kt))
  77. bb.plain("Configuration cmake fragment written to: {0}".format(d.expand("${WORKDIR}/site-file.cmake")))
  78. # restore the original config
  79. shutil.copy(config + ".orig", config)
  80. else:
  81. bb.plain("No configuration differences, skipping configuration fragment generation.")
  82. else:
  83. bb.fatal("No config files found. Did you run ccmake?")
  84. }
  85. do_ccmake_diffconfig[nostamp] = "1"
  86. do_ccmake_diffconfig[dirs] = "${B}"
  87. addtask ccmake_diffconfig