genrandconfig 19 KB

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