plugin_c_src.mk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # C source plugin.
  2. C_SRC_TARGETS = $(call list_targets,c-src)
  3. .PHONY: c-src $(C_SRC_TARGETS)
  4. c-src: $(C_SRC_TARGETS)
  5. c_src: c-src
  6. ifeq ($(PLATFORM),msys2)
  7. C_SRC_OUTPUT_SHARED_EXTENSION = .dll
  8. C_SRC_OUTPUT_STATIC_EXTENSION = .lib
  9. else
  10. C_SRC_OUTPUT_SHARED_EXTENSION = .so
  11. C_SRC_OUTPUT_STATIC_EXTENSION = .a
  12. endif
  13. c-src-makefile-change: init
  14. $i "Bootstrap a new OTP library named $(APP)"
  15. $t mkdir $(APP)/
  16. $t cp ../erlang.mk $(APP)/
  17. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  18. $i "Generate a NIF from templates"
  19. $t $(MAKE) -C $(APP) new-nif n=$(APP) $v
  20. $i "Build the application"
  21. $t $(MAKE) -C $(APP) $v
  22. $i "Touch the Makefile; check that all files get rebuilt"
  23. $t printf "%s\n" \
  24. $(APP)/$(APP).d \
  25. $(APP)/c_src/$(APP).o \
  26. $(APP)/ebin/$(APP).app \
  27. $(APP)/ebin/$(APP).beam \
  28. $(APP)/priv/$(APP)$(C_SRC_OUTPUT_SHARED_EXTENSION) \
  29. $(APP)/src/$(APP).erl | sort > $(APP)/EXPECT
  30. $t $(SLEEP)
  31. $t touch $(APP)/Makefile
  32. $t $(SLEEP)
  33. $t $(MAKE) -C $(APP) $v
  34. $t find $(APP) -type f -newer $(APP)/Makefile -not -path "$(APP)/.erlang.mk/*" | sort | diff $(APP)/EXPECT -
  35. $t rm $(APP)/EXPECT
  36. c-src-nif: init
  37. $i "Bootstrap a new OTP library named $(APP)"
  38. $t mkdir $(APP)/
  39. $t cp ../erlang.mk $(APP)/
  40. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  41. $i "Generate a NIF from templates"
  42. $t $(MAKE) -C $(APP) new-nif n=$(APP) $v
  43. $i "Build the application"
  44. $t $(MAKE) -C $(APP) $v
  45. $i "Check that all compiled files exist"
  46. $t test -f $(APP)/$(APP).d
  47. $t test -f $(APP)/c_src/$(APP).o
  48. $t test -f $(APP)/c_src/env.mk
  49. $t test -f $(APP)/ebin/$(APP).app
  50. $t test -f $(APP)/ebin/$(APP).beam
  51. $t test -f $(APP)/priv/$(APP)$(C_SRC_OUTPUT_SHARED_EXTENSION)
  52. $i "Check that the application was compiled correctly"
  53. $t $(ERL) -pa $(APP)/ebin/ -eval " \
  54. ok = application:start($(APP)), \
  55. {ok, [$(APP)]} = application:get_key($(APP), modules), \
  56. {module, $(APP)} = code:load_file($(APP)), \
  57. {hello, joe} = $(APP):hello(joe), \
  58. {hello, mike} = $(APP):hello(mike), \
  59. {hello, robert} = $(APP):hello(robert), \
  60. halt()"
  61. $i "Re-build the application"
  62. $t $(MAKE) -C $(APP) $v
  63. $i "Check that all compiled files exist"
  64. $t test -f $(APP)/$(APP).d
  65. $t test -f $(APP)/c_src/$(APP).o
  66. $t test -f $(APP)/c_src/env.mk
  67. $t test -f $(APP)/ebin/$(APP).app
  68. $t test -f $(APP)/ebin/$(APP).beam
  69. $t test -f $(APP)/priv/$(APP)$(C_SRC_OUTPUT_SHARED_EXTENSION)
  70. $i "Check that the application was compiled correctly"
  71. $t $(ERL) -pa $(APP)/ebin/ -eval " \
  72. ok = application:start($(APP)), \
  73. {ok, [$(APP)]} = application:get_key($(APP), modules), \
  74. {module, $(APP)} = code:load_file($(APP)), \
  75. {hello, joe} = $(APP):hello(joe), \
  76. {hello, mike} = $(APP):hello(mike), \
  77. {hello, robert} = $(APP):hello(robert), \
  78. halt()"
  79. $i "Clean the application"
  80. $t $(MAKE) -C $(APP) clean $v
  81. $i "Check that all intermediate files were removed"
  82. $t test ! -e $(APP)/$(APP).d
  83. $t test ! -e $(APP)/c_src/$(APP).o
  84. $t test ! -e $(APP)/ebin/$(APP).app
  85. $t test ! -e $(APP)/ebin/$(APP).beam
  86. $t test ! -e $(APP)/priv/$(APP)$(C_SRC_OUTPUT_SHARED_EXTENSION)
  87. $i "Distclean the application"
  88. $t $(MAKE) -C $(APP) distclean $v
  89. $i "Check that all files were removed"
  90. $t test ! -e $(APP)/c_src/env.mk
  91. c-src-nif-missing-name: init
  92. $i "Bootstrap a new OTP library named $(APP)"
  93. $t mkdir $(APP)/
  94. $t cp ../erlang.mk $(APP)/
  95. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  96. $i "Try to generate a NIF without giving it a name"
  97. $t ! $(MAKE) -C $(APP) new-nif $v
  98. c-src-static-nif: init
  99. $i "Bootstrap a new OTP library named $(APP)"
  100. $t mkdir $(APP)/
  101. $t cp ../erlang.mk $(APP)/
  102. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  103. $i "Generate a NIF from templates"
  104. $t $(MAKE) -C $(APP) new-nif n=$(APP) $v
  105. $i "Set C_SRC_TYPE = static in the Makefile"
  106. $t perl -ni.bak -e 'print;if ($$.==1) {print "C_SRC_TYPE = static\n"}' $(APP)/Makefile
  107. $i "Build the application"
  108. $t $(MAKE) -C $(APP) $v
  109. $i "Check that all compiled files exist"
  110. $t test -f $(APP)/$(APP).d
  111. $t test -f $(APP)/c_src/$(APP).o
  112. $t test -f $(APP)/c_src/env.mk
  113. $t test -f $(APP)/ebin/$(APP).app
  114. $t test -f $(APP)/ebin/$(APP).beam
  115. $t test -f $(APP)/priv/$(APP)$(C_SRC_OUTPUT_STATIC_EXTENSION)
  116. $i "Check that the library file contains the expected functions"
  117. $t objdump -t $(APP)/priv/test_c_src_static_nif.a | grep -c hello | grep -q 1
  118. $i "Re-build the application"
  119. $t $(MAKE) -C $(APP) $v
  120. $i "Check that all compiled files exist"
  121. $t test -f $(APP)/$(APP).d
  122. $t test -f $(APP)/c_src/$(APP).o
  123. $t test -f $(APP)/c_src/env.mk
  124. $t test -f $(APP)/ebin/$(APP).app
  125. $t test -f $(APP)/ebin/$(APP).beam
  126. $t test -f $(APP)/priv/$(APP)$(C_SRC_OUTPUT_STATIC_EXTENSION)
  127. $i "Check that the library file contains the expected functions"
  128. $t objdump -t $(APP)/priv/test_c_src_static_nif.a | grep -c hello | grep -q 1
  129. $i "Clean the application"
  130. $t $(MAKE) -C $(APP) clean $v
  131. $i "Check that all intermediate files were removed"
  132. $t test ! -e $(APP)/$(APP).d
  133. $t test ! -e $(APP)/c_src/$(APP).o
  134. $t test ! -e $(APP)/ebin/$(APP).app
  135. $t test ! -e $(APP)/ebin/$(APP).beam
  136. $t test ! -e $(APP)/priv/$(APP)$(C_SRC_OUTPUT_STATIC_EXTENSION)
  137. $i "Distclean the application"
  138. $t $(MAKE) -C $(APP) distclean $v
  139. $i "Check that all files were removed"
  140. $t test ! -e $(APP)/c_src/env.mk