2
0

utest.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # generate utest Makefile
  2. #
  3. # params:
  4. # $SRS_OBJS the objs directory to store the Makefile. ie. ./objs
  5. # $SRS_OBJS the objs directory for Makefile. ie. objs
  6. #
  7. # $APP_NAME the app name to output. ie. srs_utest
  8. # $MODULE_DIR the src dir of utest code. ie. src/utest
  9. # $LINK_OPTIONS the link options for utest. ie. -lpthread -ldl
  10. FILE=${SRS_OBJS}/${SRS_PLATFORM}/utest/Makefile
  11. # create dir for Makefile
  12. mkdir -p ${SRS_OBJS}/${SRS_PLATFORM}/utest
  13. # the prefix to generate the objs/utest/Makefile
  14. # dirs relative to current dir(objs/utest), it's trunk/objs/utest
  15. # trunk of srs, which contains the src dir, relative to objs/utest, it's trunk
  16. SRS_TRUNK_PREFIX=../../..
  17. # gest dir, relative to objs/utest, it's trunk/objs/{Platform}/gtest
  18. GTEST_DIR=../3rdparty/gtest/googletest
  19. # Whether enable C++11 or higher versions.
  20. # For linux, always use C++11 for gtest required, see https://github.com/google/googletest
  21. # For cygwin64, ignore because it use -std=gnu++11 by default.
  22. SRS_CPP_VERSION="-std=c++11"
  23. if [[ $SRS_CYGWIN64 == YES ]]; then SRS_CPP_VERSION="-std=gnu++11"; fi
  24. cat << END > ${FILE}
  25. # user must run make the ${SRS_OBJS}/utest dir
  26. # at the same dir of Makefile.
  27. # A sample Makefile for building Google Test and using it in user
  28. # tests. Please tweak it to suit your environment and project. You
  29. # may want to move it to your project's root directory.
  30. #
  31. # SYNOPSIS:
  32. #
  33. # make [all] - makes everything.
  34. # make TARGET - makes the given target.
  35. # make clean - removes all files generated by make.
  36. # Please tweak the following variable definitions as needed by your
  37. # project, except GTEST_HEADERS, which you can use in your own targets
  38. # but shouldn't modify.
  39. # Points to the root of Google Test, relative to where this file is.
  40. # Remember to tweak this if you move this file.
  41. GTEST_DIR = ${GTEST_DIR}
  42. # Where to find user code.
  43. USER_DIR = .
  44. # C++ compiler
  45. CXX = ${SRS_TOOL_CXX}
  46. # Flags passed to the preprocessor.
  47. CPPFLAGS += -I\$(GTEST_DIR)/include
  48. # Flags passed to the C++ compiler.
  49. CXXFLAGS += ${CXXFLAGS} ${UTEST_EXTRA_DEFINES}
  50. CXXFLAGS += -Wno-unused-private-field -Wno-unused-command-line-argument
  51. CXXFLAGS += ${SRS_CPP_VERSION}
  52. # All tests produced by this Makefile. Remember to add new tests you
  53. # created to the list.
  54. TESTS = ${SRS_TRUNK_PREFIX}/${SRS_OBJS}/${APP_NAME}
  55. # All Google Test headers. Usually you shouldn't change this
  56. # definition.
  57. GTEST_HEADERS = \$(GTEST_DIR)/include/gtest/*.h \\
  58. \$(GTEST_DIR)/include/gtest/internal/*.h
  59. # House-keeping build targets.
  60. all : \$(TESTS)
  61. clean :
  62. rm -f \$(TESTS) gtest.a gtest_main.a *.o
  63. # Builds gtest.a and gtest_main.a.
  64. # Usually you shouldn't tweak such internal variables, indicated by a
  65. # trailing _.
  66. GTEST_SRCS_ = \$(GTEST_DIR)/src/*.cc \$(GTEST_DIR)/src/*.h \$(GTEST_HEADERS)
  67. # For simplicity and to avoid depending on Google Test's
  68. # implementation details, the dependencies specified below are
  69. # conservative and not optimized. This is fine as Google Test
  70. # compiles fast and for ordinary users its source rarely changes.
  71. gtest-all.o : \$(GTEST_SRCS_)
  72. \$(CXX) \$(CPPFLAGS) -I\$(GTEST_DIR) \$(CXXFLAGS) -c \\
  73. \$(GTEST_DIR)/src/gtest-all.cc
  74. gtest_main.o : \$(GTEST_SRCS_)
  75. \$(CXX) \$(CPPFLAGS) -I\$(GTEST_DIR) \$(CXXFLAGS) -c \\
  76. \$(GTEST_DIR)/src/gtest_main.cc
  77. gtest.a : gtest-all.o
  78. \$(AR) \$(ARFLAGS) \$@ \$^
  79. gtest_main.a : gtest-all.o gtest_main.o
  80. \$(AR) \$(ARFLAGS) \$@ \$^
  81. # Builds a sample test. A test should link with either gtest.a or
  82. # gtest_main.a, depending on whether it defines its own main()
  83. # function.
  84. #####################################################################################
  85. #####################################################################################
  86. # SRS(Simple RTMP Server) utest section
  87. #####################################################################################
  88. #####################################################################################
  89. END
  90. #####################################################################################
  91. # Includes, the include dir.
  92. echo "# Includes, the include dir." >> ${FILE}
  93. #
  94. # current module header files
  95. echo -n "SRS_UTEST_INC = -I${SRS_TRUNK_PREFIX}/${MODULE_DIR} " >> ${FILE}
  96. #
  97. # depends module header files
  98. for item in ${MODULE_DEPENDS[*]}; do
  99. DEP_INCS_NAME="${item}_INCS"
  100. echo -n "-I${SRS_TRUNK_PREFIX}/${!DEP_INCS_NAME} " >> ${FILE}
  101. done
  102. #
  103. # depends library header files
  104. for item in ${ModuleLibIncs[*]}; do
  105. if [[ "${item:0:1}" == "/" ]]; then
  106. echo -n "-I${item} " >> ${FILE}
  107. else
  108. echo -n "-I${SRS_TRUNK_PREFIX}/${item} " >> ${FILE}
  109. fi
  110. done
  111. echo "" >> ${FILE}; echo "" >> ${FILE}
  112. #####################################################################################
  113. # Depends, the depends objects
  114. echo "# Depends, the depends objects" >> ${FILE}
  115. #
  116. # current module header files
  117. echo -n "SRS_UTEST_DEPS = " >> ${FILE}
  118. for item in ${MODULE_OBJS[*]}; do
  119. FILE_NAME=${item%.*}
  120. echo -n "${SRS_TRUNK_PREFIX}/${SRS_OBJS}/${FILE_NAME}.o " >> ${FILE}
  121. done
  122. echo "" >> ${FILE}; echo "" >> ${FILE}
  123. #
  124. echo "# Depends, utest header files" >> ${FILE}
  125. DEPS_NAME="UTEST_DEPS"
  126. echo -n "${DEPS_NAME} = " >> ${FILE}
  127. for item in ${MODULE_FILES[*]}; do
  128. HEADER_FILE="${SRS_TRUNK_PREFIX}/${MODULE_DIR}/${item}.hpp"
  129. echo -n " ${HEADER_FILE}" >> ${FILE}
  130. done
  131. echo "" >> ${FILE}; echo "" >> ${FILE}
  132. #####################################################################################
  133. # Objects, build each object of utest
  134. echo "# Objects, build each object of utest" >> ${FILE}
  135. #
  136. MODULE_OBJS=()
  137. for item in ${MODULE_FILES[*]}; do
  138. MODULE_OBJS="${MODULE_OBJS[@]} ${item}.o"
  139. cat << END >> ${FILE}
  140. ${item}.o : \$(${DEPS_NAME}) ${SRS_TRUNK_PREFIX}/${MODULE_DIR}/${item}.cpp \$(SRS_UTEST_DEPS)
  141. \$(CXX) \$(CPPFLAGS) \$(CXXFLAGS) \$(SRS_UTEST_INC) -c ${SRS_TRUNK_PREFIX}/${MODULE_DIR}/${item}.cpp -o \$@
  142. END
  143. done
  144. echo "" >> ${FILE}
  145. #####################################################################################
  146. # App for utest
  147. #
  148. # link all depends libraries
  149. echo "# link all depends libraries" >> ${FILE}
  150. echo -n "DEPS_LIBRARIES_FILES = " >> ${FILE}
  151. for item in ${ModuleLibFiles[*]}; do
  152. if [[ "${item:0:1}" == "/" ]]; then
  153. echo -n "${item} " >> ${FILE}
  154. else
  155. echo -n "${SRS_TRUNK_PREFIX}/${item} " >> ${FILE}
  156. fi
  157. done
  158. echo "" >> ${FILE}; echo "" >> ${FILE}
  159. #
  160. echo "# generate the utest binary" >> ${FILE}
  161. cat << END >> ${FILE}
  162. ${SRS_TRUNK_PREFIX}/${SRS_OBJS}/${APP_NAME} : \$(SRS_UTEST_DEPS) ${MODULE_OBJS} gtest.a
  163. \$(CXX) -o \$@ \$(CPPFLAGS) \$(CXXFLAGS) \$^ \$(DEPS_LIBRARIES_FILES) ${LINK_OPTIONS}
  164. END
  165. #####################################################################################
  166. # parent Makefile, to create module output dir before compile it.
  167. echo " @mkdir -p ${SRS_OBJS}/utest" >> ${SRS_MAKEFILE}
  168. echo -n "Generate utest ok"; echo '!';