ccmake.bbclass 3.7 KB

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