use_requirements.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/python
  2. # Copyright 2003 Dave Abrahams
  3. # Copyright 2002, 2003, 2004, 2006 Vladimir Prus
  4. # Distributed under the Boost Software License, Version 1.0.
  5. # (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
  6. import BoostBuild
  7. t = BoostBuild.Tester(use_test_config=False)
  8. # Test that usage requirements on main targets work (and are propagated all the
  9. # way up, and not only to direct dependants).
  10. t.write("jamroot.jam", "")
  11. # Note: 'lib cc ..', not 'lib c'. If using 'lib c: ...' the HP-CXX linker will
  12. # confuse it with the system C runtime.
  13. t.write("jamfile.jam", """\
  14. lib b : b.cpp : <link>shared:<define>SHARED_B : :
  15. <define>FOO <link>shared:<define>SHARED_B ;
  16. lib cc : c.cpp b ;
  17. exe a : a.cpp cc ;
  18. """)
  19. t.write("b.cpp", """\
  20. void
  21. #if defined(_WIN32) && defined(SHARED_B)
  22. __declspec(dllexport)
  23. #endif
  24. foo() {}
  25. """)
  26. t.write("c.cpp", """\
  27. void
  28. #if defined(_WIN32) && defined(SHARED_B)
  29. __declspec(dllexport)
  30. #endif
  31. create_lib_please() {}
  32. """)
  33. t.write("a.cpp", """\
  34. #ifdef FOO
  35. void
  36. # if defined(_WIN32) && defined(SHARED_B)
  37. __declspec(dllexport)
  38. # endif
  39. foo() {}
  40. #endif
  41. int main() { foo(); }
  42. """)
  43. t.run_build_system()
  44. t.run_build_system(["--clean"])
  45. # Test that use requirements on main target work, when they are referred using
  46. # 'dependency' features.
  47. t.write("jamfile.jam", """\
  48. lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
  49. <link>shared:<define>SHARED_B ;
  50. exe a : a.cpp : <use>b ;
  51. """)
  52. t.write("b.cpp", """\
  53. void
  54. #if defined(_WIN32) && defined(SHARED_B)
  55. __declspec(dllexport)
  56. #endif
  57. foo() {}
  58. """)
  59. t.write("a.cpp", """\
  60. #ifdef FOO
  61. int main() {}
  62. #endif
  63. """)
  64. t.run_build_system()
  65. t.run_build_system(["--clean"])
  66. # Test that usage requirements on a project work.
  67. t.write("jamfile.jam", "exe a : a.cpp lib//b ;")
  68. t.write("lib/jamfile.jam", """\
  69. project
  70. : requirements <link>shared:<define>SHARED_B
  71. : usage-requirements <define>FOO <link>shared:<define>SHARED_B ;
  72. lib b : b.cpp ;
  73. """)
  74. t.write("lib/b.cpp", """\
  75. void
  76. #if defined(_WIN32) && defined(SHARED_B)
  77. __declspec(dllexport)
  78. #endif
  79. foo() {}
  80. """)
  81. t.run_build_system()
  82. # Test that use requirements are inherited correctly.
  83. t.write("jamfile.jam", "exe a : a.cpp lib/1//b ;")
  84. t.write("a.cpp", """\
  85. #if defined(FOO) && defined(ZOO)
  86. void foo() {}
  87. #endif
  88. int main() { foo(); }
  89. """)
  90. t.write("lib/jamfile.jam", """\
  91. project : requirements : usage-requirements <define>FOO ;
  92. """)
  93. t.write("lib/1/jamfile.jam", """\
  94. project
  95. : requirements <link>shared:<define>SHARED_B
  96. : usage-requirements <define>ZOO <link>shared:<define>SHARED_B ;
  97. lib b : b.cpp ;
  98. """)
  99. t.write("lib/1/b.cpp", """\
  100. void
  101. #if defined(_WIN32) && defined(SHARED_B)
  102. __declspec(dllexport)
  103. #endif
  104. foo() {}
  105. """)
  106. t.run_build_system()
  107. t.run_build_system(["--clean"])
  108. # Test that we correctly handle dependency features in usage requirements on
  109. # target.
  110. t.write("jamfile.jam", """\
  111. lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
  112. <link>shared:<define>SHARED_B ;
  113. # Here's the test: we should correctly handle dependency feature and get usage
  114. # requirements from 'b'.
  115. lib cc : c.cpp : <link>shared:<define>SHARED_C : : <library>b ;
  116. # This will build only if <define>FOO was propagated from 'c'.
  117. exe a : a.cpp cc ;
  118. """)
  119. t.write("a.cpp", """\
  120. #ifdef FOO
  121. void
  122. # if defined(_WIN32) && defined(SHARED_B)
  123. __declspec(dllexport)
  124. # endif
  125. foo();
  126. #endif
  127. int main() { foo(); }
  128. """)
  129. t.write("c.cpp", """\
  130. int
  131. #if defined(_WIN32) && defined(SHARED_C)
  132. __declspec(dllexport)
  133. #endif
  134. must_export_something;
  135. """)
  136. t.run_build_system()
  137. t.run_build_system(["--clean"])
  138. # Test correct handling of dependency features in project requirements.
  139. t.write("jamfile.jam", "exe a : a.cpp lib1//cc ;")
  140. t.write("lib1/jamfile.jam", """\
  141. project
  142. : requirements <link>shared:<define>SHARED_C
  143. : usage-requirements <library>../lib2//b <link>shared:<define>SHARED_C ;
  144. lib cc : c.cpp ;
  145. """)
  146. t.write("lib1/c.cpp", """\
  147. int
  148. #if defined(_WIN32) && defined(SHARED_C)
  149. __declspec(dllexport)
  150. #endif
  151. must_export_something;
  152. """)
  153. t.write("lib2/jamfile.jam", """\
  154. lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
  155. <link>shared:<define>SHARED_B ;
  156. """)
  157. t.copy("b.cpp", "lib2/b.cpp")
  158. t.run_build_system()
  159. # Test that targets listed in dependency features in usage requirements are
  160. # built with the correct properties.
  161. t.rm(".")
  162. t.write("jamroot.jam", "")
  163. t.write("jamfile.jam", """\
  164. lib main : main.cpp : <use>libs//lib1 : : <library>libs//lib1 ;
  165. exe hello : hello.cpp main : ;
  166. """)
  167. t.write("main.cpp", """\
  168. void
  169. #if defined(_WIN32) && defined(SHARED_LIB1)
  170. __declspec(dllimport)
  171. #endif
  172. foo();
  173. int main() { foo(); }
  174. """)
  175. t.write("hello.cpp", "\n")
  176. t.write("libs/a.cpp", """\
  177. void
  178. #if defined(_WIN32) && defined(SHARED_LIB1)
  179. __declspec(dllexport)
  180. #endif
  181. foo() {}
  182. """)
  183. # This library should be built with the same properties as 'main'. This is a
  184. # regression test for a bug when they were generated with empty properties, and
  185. # there were ambiguities between variants.
  186. t.write("libs/jamfile.jam", """\
  187. lib lib1 : a_d.cpp : <variant>debug <link>shared:<define>SHARED_LIB1 : :
  188. <link>shared:<define>SHARED_LIB1 ;
  189. lib lib1 : a.cpp : <variant>release <link>shared:<define>SHARED_LIB1 : :
  190. <link>shared:<define>SHARED_LIB1 ;
  191. """)
  192. t.write("libs/a_d.cpp", """\
  193. void
  194. #if defined(_WIN32) && defined(SHARED_LIB1)
  195. __declspec(dllexport)
  196. #endif
  197. foo() {}
  198. """)
  199. t.run_build_system(["link=static"])
  200. t.expect_addition("libs/bin/$toolset/debug/link-static*/a_d.obj")
  201. # Test that indirect conditionals are respected in usage requirements.
  202. t.rm(".")
  203. t.write("jamroot.jam", """\
  204. rule has-foo ( properties * ) { return <define>HAS_FOO ; }
  205. exe a : a.cpp b ;
  206. lib b : b.cpp : <link>static : : <conditional>@has-foo ;
  207. """)
  208. t.write("a.cpp", """\
  209. #ifdef HAS_FOO
  210. void foo();
  211. int main() { foo(); }
  212. #endif
  213. """)
  214. t.write("b.cpp", """\
  215. void
  216. #if defined(_WIN32) && defined(SHARED_B)
  217. __declspec(dllexport)
  218. #endif
  219. foo() {}
  220. """)
  221. t.run_build_system()
  222. t.expect_addition("bin/$toolset/debug*/a.exe")
  223. t.cleanup()