PageRenderTime 70ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Etc/scripts/CMakeListGen.rb

https://github.com/vonami/inet-flc
Ruby | 219 lines | 142 code | 45 blank | 32 comment | 11 complexity | 438168eb02ac88df5e4d44a301449363 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. #
  3. # Author: Johnny Lai
  4. # Copyright (c) 2004 Johnny Lai
  5. #
  6. # =DESCRIPTION
  7. # CMakeLists.txt generater for omnetpp type projects. Currently works with INET
  8. # and generates IPv6Suite's OneBigExe.cmake but should work for others too.
  9. #
  10. #Pattern for doing wildcard matches of filenames recursively i.e. subdirectories too
  11. RECURSEDIR="**/*"
  12. #Extension of files used for message subclassing
  13. MSGEXT=".msg"
  14. def traverseDirectory(dir, expression, ignore = nil)
  15. oldpwd = Dir.pwd
  16. Dir.chdir(dir)
  17. arr = Array.new
  18. Dir[expression].each {|f|
  19. next if ignore and f =~ Regexp.new(ignore)
  20. arr.push(f)
  21. }
  22. arr
  23. ensure
  24. Dir.chdir(oldpwd)
  25. end
  26. def customCommands(dir)
  27. m = traverseDirectory(dir,RECURSEDIR+MSGEXT,"RTP|Unsupported")
  28. genSources = Array.new
  29. cleanSources = Array.new
  30. objs = Array.new
  31. string = ""
  32. m.each{|msg|
  33. cfile = msg.to_s.gsub(MSGEXT,"_m.cc")
  34. hfile = msg.to_s.gsub(MSGEXT,"_m.h")
  35. ofile = msg.to_s.gsub(MSGEXT, "_m.o")
  36. #Needed as we do opp_msgc gen and rest of build from top level dir only
  37. cfile = File.basename cfile
  38. hfile = File.basename hfile
  39. ofile = File.basename ofile
  40. genSources.push(cfile)
  41. cleanSources.push(hfile)
  42. objs.push(ofile)
  43. # ADD_CUSTOM_COMMAND(TARGET #{cwd} PRE_BUILD COMMAND ${OPP_MSGC} ARGS -h #{m})
  44. }
  45. #string += "\nOPP_WRAP_MSGC(dum dum2 #{m.join("\n")}\n)\n"
  46. string += "\nOPP_WRAP_MSGC_ALL()\n"
  47. Array[string, genSources]
  48. end
  49. def addSourceFiles(dir, ignorePattern)
  50. c = traverseDirectory(dir, RECURSEDIR+".{h,cc,cpp,c}", ignorePattern)
  51. includeDirs = Array.new
  52. c.delete_if {|f|
  53. header = f =~ /\.h$/
  54. includeDirs.push(File.dirname(f)) if header and not includeDirs.include? File.dirname(f)
  55. header
  56. }
  57. Array[c, includeDirs]
  58. end
  59. # Used only by IPv6Suite
  60. def readSourceList(filename)
  61. @sourceList = IO.readlines(filename)
  62. @sourceList.map! {|e|
  63. e.chomp!
  64. #Remove leading path so we match only on file component
  65. e.gsub!(/^.*\//,"")
  66. e
  67. }
  68. @sourceList.delete_if{|e| not e =~ /[[:alpha:]]/}
  69. return @sourceList
  70. end
  71. def addTests(dir)
  72. c = traverseDirectory(dir, RECURSEDIR + ".test")
  73. testDirs = Array.new
  74. c.each{ |test|
  75. testDirs.push(File.dirname(test)) if not testDirs.include? File.dirname(test)
  76. }
  77. testDirs
  78. end
  79. def writeTest(testDirs, projName)
  80. testDirs.each{ |d|
  81. open("#{d}/CMakeLists.txt","w") { |testCMake|
  82. testCMake.puts "LINK_LIBRARIES(#{projName} ${OPP_LIBRARIES})"
  83. testCMake.puts "OPP_WRAP_TEST(#{File.basename(d)})"
  84. }
  85. }
  86. end
  87. def writeCMakeList(dir, outputName, projName = nil)
  88. commonIgnore = "Unsupported|_m\.|test"
  89. ignore = @customise ? "TCP|" + commonIgnore : "RTP|" + commonIgnore
  90. sources, includes = addSourceFiles(dir, ignore)
  91. projName ||= File.basename(dir)
  92. open("#{dir}/#{outputName}","w") { |x|
  93. x.puts "# -*- CMAKE -*-"
  94. x.puts %{#Generated by "#{$0} #{ARGV.join(" ")}"}
  95. if not @customise
  96. x.puts(sprintf("PROJECT(%s)", projName)) if projName and projName.length > 0
  97. # set_dir_props generated from customCommands requires this
  98. x.puts %{CMAKE_MINIMUM_REQUIRED(VERSION 2.0)}
  99. x.puts "SET(CMAKEFILES_PATH #{File.dirname(File.dirname($0))+"/CMake"})"
  100. x.puts %{OPTION(BUILD_SHARED_LIBS "Build with shared libraries." ON)}
  101. x.puts %{SET(ONE_BIG_EXE ON)}
  102. x.puts("INCLUDE(${CMAKEFILES_PATH}/FindOmnet.cmake)")
  103. x.puts("INCLUDE_DIRECTORIES(${OPP_INCLUDE_PATH})")
  104. x.puts("INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR})")
  105. end
  106. customCommandsLines, genSources = customCommands(dir)
  107. x.print customCommandsLines
  108. #It appears that these source files properties only exist in the current
  109. #Dir/cmakelist.txt because in subdir's cmakelist.txt cannot use the source
  110. #file here as it complains source does not exist unless we also set generated property in there for these files again
  111. #this does not work however
  112. #x.print "SET_SOURCE_FILES_PROPERTIES(${GENERATED_MSGC_FILES} GENERATED PROPERTIES COMPILE_FLAGS -Wall)\n\n"
  113. x.print "\nSET( ", projName, "_SRCS\n"
  114. if @customise
  115. #Do special inclusion of files only found in sourceList returned from
  116. #readSourceList (IPv6Suite one huge statc executable)
  117. readSourceList("#{dir}/sourcelist")
  118. sources.delete_if {|e|
  119. ret = true
  120. @sourceList.each{|y|
  121. if e =~ Regexp.new("#{y}$")
  122. ret = false
  123. #Must have been deleting wrong ones as other files were missing (premature optimisation) since some
  124. #files have similar postfix names
  125. # @sourceList.delete(y){|z|
  126. # $stderr.puts "Unable to remove element #{y} from @sourceList"
  127. #}
  128. break
  129. end
  130. }
  131. ret
  132. }
  133. end
  134. #necessary otherwise any subsequent SUBDIRS commands will change
  135. #the relative source file to an incorrect absolute path
  136. basepath="${PROJECT_SOURCE_DIR}/"
  137. sources.each{|c|
  138. x.puts basepath + c
  139. }
  140. x.puts ")"
  141. x.puts "SET_SOURCE_FILES_PROPERTIES(${#{projName}_SRCS} PROPERTIES COMPILE_FLAGS -Wall)"
  142. x.puts "SET(#{projName}_SRCS ${GENERATED_MSGC_FILES} ${#{projName}_SRCS})"
  143. x.puts
  144. x.puts
  145. x.puts "INCLUDE_DIRECTORIES("
  146. includes.each{|inc|
  147. x.puts basepath + inc
  148. }
  149. x.puts ")\n\n"
  150. if not @customise
  151. outputdir = projName == "INET" ? "Examples/bin" : "."
  152. x.puts %{SET(OUTPUTDIR #{outputdir}) }
  153. x.puts(sprintf("ADD_LIBRARY(%s ${%s})\n", projName, projName + "_SRCS"))
  154. x.puts "SET(#{projName} ${OUTPUTDIR}/#{projName})"
  155. x.puts "ADD_EXECUTABLE(${#{projName}} ${#{projName}_SRCS})"
  156. x.puts %{TARGET_LINK_LIBRARIES(${#{projName}} ${OPP_LIBRARIES} -lstdc++)} # abstract libs
  157. x.puts "SET(tk#{projName} ${OUTPUTDIR}/tk#{projName})"
  158. x.puts "ADD_EXECUTABLE(${tk#{projName}} ${#{projName}_SRCS})"
  159. x.puts %{TARGET_LINK_LIBRARIES(${tk#{projName}} ${OPP_TKGUILIBRARIES} -lstdc++)}
  160. end
  161. }
  162. end
  163. ## main
  164. if ARGV.length < 2 then
  165. print "Usage ", " <source dir name> <Project Name>\n", \
  166. " where [source dir] is where MakeLists.txt will be generated for\n", \
  167. "Generated in current working directory"
  168. exit
  169. else
  170. projName = ARGV[1]
  171. @customise = projName == "IPv6Suite"
  172. outname = @customise ? "OneBigStaticExe.cmake" : "CMakeLists.txt"
  173. writeCMakeList(ARGV[0], outname, projName)
  174. end