genrandconfig 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #!/usr/bin/env python
  2. # Copyright (C) 2014 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. # This script generates a random configuration for testing Buildroot.
  18. from __future__ import print_function
  19. import contextlib
  20. import csv
  21. import os
  22. from random import randint
  23. import subprocess
  24. import sys
  25. from distutils.version import StrictVersion
  26. import platform
  27. if sys.hexversion >= 0x3000000:
  28. import urllib.request as _urllib
  29. else:
  30. import urllib2 as _urllib
  31. def urlopen_closing(uri):
  32. return contextlib.closing(_urllib.urlopen(uri))
  33. if sys.hexversion >= 0x3000000:
  34. def decode_byte_list(bl):
  35. return [b.decode() for b in bl]
  36. else:
  37. def decode_byte_list(e):
  38. return e
  39. class SystemInfo:
  40. DEFAULT_NEEDED_PROGS = ["make", "git", "gcc", "timeout"]
  41. DEFAULT_OPTIONAL_PROGS = ["bzr", "java", "javac", "jar"]
  42. def __init__(self):
  43. self.needed_progs = list(self.__class__.DEFAULT_NEEDED_PROGS)
  44. self.optional_progs = list(self.__class__.DEFAULT_OPTIONAL_PROGS)
  45. self.progs = {}
  46. def find_prog(self, name, flags=os.X_OK, env=os.environ):
  47. if not name or name[0] == os.sep:
  48. raise ValueError(name)
  49. prog_path = env.get("PATH", None)
  50. # for windows compatibility, we'd need to take PATHEXT into account
  51. if prog_path:
  52. for prog_dir in filter(None, prog_path.split(os.pathsep)):
  53. # os.join() not necessary: non-empty prog_dir
  54. # and name[0] != os.sep
  55. prog = prog_dir + os.sep + name
  56. if os.access(prog, flags):
  57. return prog
  58. # --
  59. return None
  60. def has(self, prog):
  61. """Checks whether a program is available.
  62. Lazily evaluates missing entries.
  63. Returns: None if prog not found, else path to the program [evaluates
  64. to True]
  65. """
  66. try:
  67. return self.progs[prog]
  68. except KeyError:
  69. pass
  70. have_it = self.find_prog(prog)
  71. # java[c] needs special care
  72. if have_it and prog in ('java', 'javac'):
  73. with open(os.devnull, "w") as devnull:
  74. if subprocess.call("%s -version | grep gcj" % prog,
  75. shell=True,
  76. stdout=devnull, stderr=devnull) != 1:
  77. have_it = False
  78. # --
  79. self.progs[prog] = have_it
  80. return have_it
  81. def check_requirements(self):
  82. """Checks program dependencies.
  83. Returns: True if all mandatory programs are present, else False.
  84. """
  85. do_check_has_prog = self.has
  86. missing_requirements = False
  87. for prog in self.needed_progs:
  88. if not do_check_has_prog(prog):
  89. print("ERROR: your system lacks the '%s' program" % prog)
  90. missing_requirements = True
  91. # check optional programs here,
  92. # else they'd get checked by each worker instance
  93. for prog in self.optional_progs:
  94. do_check_has_prog(prog)
  95. return not missing_requirements
  96. def get_toolchain_configs(toolchains_csv, buildrootdir):
  97. """Fetch and return the possible toolchain configurations
  98. This function returns an array of toolchain configurations. Each
  99. toolchain configuration is itself an array of lines of the defconfig.
  100. """
  101. with open(toolchains_csv) as r:
  102. # filter empty lines and comments
  103. lines = [ t for t in r.readlines() if len(t.strip()) > 0 and t[0] != '#' ]
  104. toolchains = decode_byte_list(lines)
  105. configs = []
  106. (_, _, _, _, hostarch) = os.uname()
  107. # ~2015 distros report x86 when on a 32bit install
  108. if hostarch == 'i686' or hostarch == 'i386' or hostarch == 'x86':
  109. hostarch = 'x86'
  110. for row in csv.reader(toolchains):
  111. config = {}
  112. configfile = row[0]
  113. config_hostarch = row[1]
  114. keep = False
  115. # Keep all toolchain configs that work regardless of the host
  116. # architecture
  117. if config_hostarch == "any":
  118. keep = True
  119. # Keep all toolchain configs that can work on the current host
  120. # architecture
  121. if hostarch == config_hostarch:
  122. keep = True
  123. # Assume that x86 32 bits toolchains work on x86_64 build
  124. # machines
  125. if hostarch == 'x86_64' and config_hostarch == "x86":
  126. keep = True
  127. if not keep:
  128. continue
  129. if not os.path.isabs(configfile):
  130. configfile = os.path.join(buildrootdir, configfile)
  131. with open(configfile) as r:
  132. config = r.readlines()
  133. configs.append(config)
  134. return configs
  135. def is_toolchain_usable(configfile, config):
  136. """Check if the toolchain is actually usable."""
  137. with open(configfile) as configf:
  138. configlines = configf.readlines()
  139. # Check that the toolchain configuration is still present
  140. for toolchainline in config:
  141. if toolchainline not in configlines:
  142. print("WARN: toolchain can't be used", file=sys.stderr)
  143. print(" Missing: %s" % toolchainline.strip(), file=sys.stderr)
  144. return False
  145. # The latest Linaro toolchains on x86-64 hosts requires glibc
  146. # 2.14+ on the host.
  147. if platform.machine() == 'x86_64':
  148. if 'BR2_TOOLCHAIN_EXTERNAL_LINARO_ARM=y\n' in configlines or \
  149. 'BR2_TOOLCHAIN_EXTERNAL_LINARO_AARCH64=y\n' in configlines or \
  150. 'BR2_TOOLCHAIN_EXTERNAL_LINARO_ARMEB=y\n' in configlines:
  151. ldd_version_output = subprocess.check_output(['ldd', '--version'])
  152. glibc_version = ldd_version_output.splitlines()[0].split()[-1]
  153. if StrictVersion('2.14') > StrictVersion(glibc_version):
  154. print("WARN: ignoring the Linaro ARM toolchains because too old host glibc", file=sys.stderr)
  155. return False
  156. return True
  157. def fixup_config(configfile):
  158. """Finalize the configuration and reject any problematic combinations
  159. This function returns 'True' when the configuration has been
  160. accepted, and 'False' when the configuration has not been accepted because
  161. it is known to fail (in which case another random configuration will be
  162. generated).
  163. """
  164. sysinfo = SystemInfo()
  165. with open(configfile) as configf:
  166. configlines = configf.readlines()
  167. if "BR2_NEEDS_HOST_JAVA=y\n" in configlines and not sysinfo.has("java"):
  168. return False
  169. if "BR2_NEEDS_HOST_JAVAC=y\n" in configlines and not sysinfo.has("javac"):
  170. return False
  171. if "BR2_NEEDS_HOST_JAR=y\n" in configlines and not sysinfo.has("jar"):
  172. return False
  173. # python-nfc needs bzr
  174. if 'BR2_PACKAGE_PYTHON_NFC=y\n' in configlines and not sysinfo.has("bzr"):
  175. return False
  176. # The ctng toolchain is affected by PR58854
  177. if 'BR2_PACKAGE_LTTNG_TOOLS=y\n' in configlines and \
  178. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/armv5-ctng-linux-gnueabi.tar.xz"\n' in configlines:
  179. return False
  180. # The ctng toolchain tigger an assembler error with guile package when compiled with -Os (same issue as for CS ARM 2014.05-29)
  181. if 'BR2_PACKAGE_GUILE=y\n' in configlines and \
  182. 'BR2_OPTIMIZE_S=y\n' in configlines and \
  183. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/armv5-ctng-linux-gnueabi.tar.xz"\n' in configlines:
  184. return False
  185. # The ctng toolchain is affected by PR58854
  186. if 'BR2_PACKAGE_LTTNG_TOOLS=y\n' in configlines and \
  187. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/armv6-ctng-linux-uclibcgnueabi.tar.xz"\n' in configlines:
  188. return False
  189. # The ctng toolchain is affected by PR58854
  190. if 'BR2_PACKAGE_LTTNG_TOOLS=y\n' in configlines and \
  191. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/armv7-ctng-linux-gnueabihf.tar.xz"\n' in configlines:
  192. return False
  193. # The ctng toolchain is affected by PR60155
  194. if 'BR2_PACKAGE_SDL=y\n' in configlines and \
  195. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/powerpc-ctng-linux-uclibc.tar.xz"\n' in configlines:
  196. return False
  197. # The ctng toolchain is affected by PR60155
  198. if 'BR2_PACKAGE_LIBMPEG2=y\n' in configlines and \
  199. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/powerpc-ctng-linux-uclibc.tar.xz"\n' in configlines:
  200. return False
  201. # This MIPS toolchain uses eglibc-2.18 which lacks SYS_getdents64
  202. if 'BR2_PACKAGE_STRONGSWAN=y\n' in configlines and \
  203. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mips64el-ctng_n64-linux-gnu.tar.xz"\n' in configlines:
  204. return False
  205. # This MIPS toolchain uses eglibc-2.18 which lacks SYS_getdents64
  206. if 'BR2_PACKAGE_PYTHON3=y\n' in configlines and \
  207. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mips64el-ctng_n64-linux-gnu.tar.xz"\n' in configlines:
  208. return False
  209. # libffi not available on sh2a and ARMv7-M, but propagating libffi
  210. # arch dependencies in Buildroot is really too much work, so we
  211. # handle this here.
  212. if 'BR2_sh2a=y\n' in configlines and \
  213. 'BR2_PACKAGE_LIBFFI=y\n' in configlines:
  214. return False
  215. if 'BR2_ARM_CPU_ARMV7M=y\n' in configlines and \
  216. 'BR2_PACKAGE_LIBFFI=y\n' in configlines:
  217. return False
  218. if 'BR2_PACKAGE_SUNXI_BOARDS=y\n' in configlines:
  219. configlines.remove('BR2_PACKAGE_SUNXI_BOARDS_FEX_FILE=""\n')
  220. configlines.append('BR2_PACKAGE_SUNXI_BOARDS_FEX_FILE="a10/hackberry.fex"\n')
  221. # This MIPS uClibc toolchain fails to build the gdb package
  222. if 'BR2_PACKAGE_GDB=y\n' in configlines and \
  223. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mipsel-ctng-linux-uclibc.tar.xz"\n' in configlines:
  224. return False
  225. # This MIPS uClibc toolchain fails to build the rt-tests package
  226. if 'BR2_PACKAGE_RT_TESTS=y\n' in configlines and \
  227. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mipsel-ctng-linux-uclibc.tar.xz"\n' in configlines:
  228. return False
  229. # This MIPS uClibc toolchain fails to build the civetweb package
  230. if 'BR2_PACKAGE_CIVETWEB=y\n' in configlines and \
  231. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mipsel-ctng-linux-uclibc.tar.xz"\n' in configlines:
  232. return False
  233. # This MIPS ctng toolchain fails to build the python3 package
  234. if 'BR2_PACKAGE_PYTHON3=y\n' in configlines and \
  235. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mips64el-ctng_n64-linux-gnu.tar.xz"\n' in configlines:
  236. return False
  237. # This MIPS uClibc toolchain fails to build the strace package
  238. if 'BR2_PACKAGE_STRACE=y\n' in configlines and \
  239. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mipsel-ctng-linux-uclibc.tar.xz"\n' in configlines:
  240. return False
  241. # This MIPS uClibc toolchain fails to build the cdrkit package
  242. if 'BR2_PACKAGE_CDRKIT=y\n' in configlines and \
  243. 'BR2_STATIC_LIBS=y\n' in configlines and \
  244. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mipsel-ctng-linux-uclibc.tar.xz"\n' in configlines:
  245. return False
  246. # uClibc vfork static linking issue
  247. if 'BR2_PACKAGE_ALSA_LIB=y\n' in configlines and \
  248. 'BR2_STATIC_LIBS=y\n' in configlines and \
  249. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/i486-ctng-linux-uclibc.tar.xz"\n' in configlines:
  250. return False
  251. # This MIPS uClibc toolchain fails to build the weston package
  252. if 'BR2_PACKAGE_WESTON=y\n' in configlines and \
  253. 'BR2_TOOLCHAIN_EXTERNAL_URL="http://autobuild.buildroot.org/toolchains/tarballs/mipsel-ctng-linux-uclibc.tar.xz"\n' in configlines:
  254. return False
  255. # The cs nios2 2017.02 toolchain is affected by binutils PR19405
  256. if 'BR2_TOOLCHAIN_EXTERNAL_CODESOURCERY_NIOSII=y\n' in configlines and \
  257. 'BR2_PACKAGE_BOOST=y\n' in configlines:
  258. return False
  259. # The cs nios2 2017.02 toolchain is affected by binutils PR19405
  260. if 'BR2_TOOLCHAIN_EXTERNAL_CODESOURCERY_NIOSII=y\n' in configlines and \
  261. 'BR2_PACKAGE_QT5BASE_GUI=y\n' in configlines:
  262. return False
  263. # The cs nios2 2017.02 toolchain is affected by binutils PR19405
  264. if 'BR2_TOOLCHAIN_EXTERNAL_CODESOURCERY_NIOSII=y\n' in configlines and \
  265. 'BR2_PACKAGE_QT_GUI_MODULE=y\n' in configlines:
  266. return False
  267. # The cs nios2 2017.02 toolchain is affected by binutils PR19405
  268. if 'BR2_TOOLCHAIN_EXTERNAL_CODESOURCERY_NIOSII=y\n' in configlines and \
  269. 'BR2_PACKAGE_FLANN=y\n' in configlines:
  270. return False
  271. # or1k affected by binutils PR21464
  272. if 'BR2_or1k=y\n' in configlines and \
  273. 'BR2_PACKAGE_QT_GUI_MODULE=y\n' in configlines:
  274. return False
  275. with open(configfile, "w+") as configf:
  276. configf.writelines(configlines)
  277. return True
  278. def gen_config(args):
  279. """Generate a new random configuration
  280. This function generates the configuration, by choosing a random
  281. toolchain configuration and then generating a random selection of
  282. packages.
  283. """
  284. # Select a random toolchain configuration
  285. configs = get_toolchain_configs(args.toolchains_csv, args.buildrootdir)
  286. i = randint(0, len(configs) - 1)
  287. toolchainconfig = configs[i]
  288. configlines = list(toolchainconfig)
  289. # Combine with the minimal configuration
  290. minimalconfigfile = os.path.join(args.buildrootdir,
  291. 'support/config-fragments/minimal.config')
  292. with open(minimalconfigfile) as minimalf:
  293. configlines += minimalf.readlines()
  294. # Allow hosts with old certificates to download over https
  295. configlines.append("BR2_WGET=\"wget --passive-ftp -nd -t 3 --no-check-certificate\"\n")
  296. # Amend the configuration with a few things.
  297. if randint(0, 20) == 0:
  298. configlines.append("BR2_ENABLE_DEBUG=y\n")
  299. if randint(0, 1) == 0:
  300. configlines.append("BR2_INIT_BUSYBOX=y\n")
  301. elif randint(0, 15) == 0:
  302. configlines.append("BR2_INIT_SYSTEMD=y\n")
  303. elif randint(0, 10) == 0:
  304. configlines.append("BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEV=y\n")
  305. if randint(0, 20) == 0:
  306. configlines.append("BR2_STATIC_LIBS=y\n")
  307. if randint(0, 20) == 0:
  308. configlines.append("BR2_PACKAGE_PYTHON_PY_ONLY=y\n")
  309. # Write out the configuration file
  310. if not os.path.exists(args.outputdir):
  311. os.makedirs(args.outputdir)
  312. if args.outputdir == os.path.abspath(os.path.join(args.buildrootdir, "output")):
  313. configfile = os.path.join(args.buildrootdir, ".config")
  314. else:
  315. configfile = os.path.join(args.outputdir, ".config")
  316. with open(configfile, "w+") as configf:
  317. configf.writelines(configlines)
  318. subprocess.check_call(["make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
  319. "olddefconfig"])
  320. if not is_toolchain_usable(configfile, toolchainconfig):
  321. return 2
  322. # Now, generate the random selection of packages, and fixup
  323. # things if needed.
  324. # Safe-guard, in case we can not quickly come to a valid
  325. # configuration: allow at most 100 (arbitrary) iterations.
  326. bounded_loop = 100
  327. while True:
  328. if bounded_loop == 0:
  329. print("ERROR: cannot generate random configuration after 100 iterations",
  330. file=sys.stderr)
  331. return 1
  332. bounded_loop -= 1
  333. subprocess.check_call(["make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
  334. "KCONFIG_PROBABILITY=%d" % randint(1, 30),
  335. "randpackageconfig"])
  336. if fixup_config(configfile):
  337. break
  338. subprocess.check_call(["make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
  339. "olddefconfig"])
  340. subprocess.check_call(["make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
  341. "savedefconfig"])
  342. return subprocess.call(["make", "O=%s" % args.outputdir, "-C", args.buildrootdir,
  343. "core-dependencies"])
  344. if __name__ == '__main__':
  345. import argparse
  346. parser = argparse.ArgumentParser(description="Generate a random configuration")
  347. parser.add_argument("--outputdir", "-o",
  348. help="Output directory (relative to current directory)",
  349. type=str, default='output')
  350. parser.add_argument("--buildrootdir", "-b",
  351. help="Buildroot directory (relative to current directory)",
  352. type=str, default='.')
  353. parser.add_argument("--toolchains-csv",
  354. help="Path of the toolchain configuration file",
  355. type=str,
  356. default="support/config-fragments/autobuild/toolchain-configs.csv")
  357. args = parser.parse_args()
  358. # We need the absolute path to use with O=, because the relative
  359. # path to the output directory here is not relative to the
  360. # Buildroot sources, but to the current directory.
  361. args.outputdir = os.path.abspath(args.outputdir)
  362. try:
  363. ret = gen_config(args)
  364. except Exception as e:
  365. print(str(e), file=sys.stderr)
  366. parser.exit(1)
  367. parser.exit(ret)