debcreation.nim 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #
  2. #
  3. # The Nim Installation Generator
  4. # (c) Copyright 2012 Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import osproc, times, os, strutils
  10. # http://www.debian.org/doc/manuals/maint-guide/
  11. # Required files for debhelper.
  12. # -- control
  13. # -- copyright
  14. # -- changelog
  15. # -- rules
  16. type
  17. TDebOptions* = object
  18. buildDepends*, pkgDepends*, shortDesc*: string
  19. licenses*: seq[tuple[files, license: string]]
  20. template addN(r: string) =
  21. result.add(r)
  22. result.add("\n")
  23. proc createControl(pkgName, maintainer, shortDesc, desc: string,
  24. buildDepends, pkgDepends: string = ""): string =
  25. ## pkgName: Should be the package name, no spaces.
  26. ## maintainer: firstName lastName <email>
  27. ## shortDesc: short description of the application
  28. ## desc: long description of the application
  29. ## buildDepends: what the build depends on (compiling from source),
  30. ## this needs to be in the format deb accepts. For example,
  31. ## for gcc: ``gcc (>= 4:4.3.2)``
  32. ## Multiple dependencies should be separated by commas.
  33. ## pkgDepends: Same as buildDepends except that this specifies the
  34. ## dependencies that the compiled application depends on.
  35. result = ""
  36. addN("Source: " & pkgName)
  37. addN("Maintainer: " & maintainer)
  38. addN("Section: misc")
  39. addN("Priority: optional")
  40. addN("Standards-Version: 3.9.2")
  41. addN("Build-Depends: debhelper (>= 8)" &
  42. (if buildDepends != "": ", " & buildDepends else: ""))
  43. addN("\n")
  44. addN("Package: " & pkgName)
  45. addN("Architecture: any")
  46. addN("Depends: ${shlibs:Depends}, ${misc:Depends}" &
  47. (if pkgDepends != "": ", " & pkgDepends else: ""))
  48. var formattedDesc = ""
  49. for line in splitLines(desc):
  50. if line == "":
  51. formattedDesc.add(" .\n")
  52. else:
  53. formattedDesc.add(" " & line & "\n")
  54. addN("Description: " & shortDesc & "\n" & formattedDesc)
  55. proc createCopyright(pkgName, mtnName, mtnEmail, version: string,
  56. licenses: seq[tuple[files, license: string]]): string =
  57. ## pkgName: Package name
  58. ## mtnName: Maintainer name
  59. ## mtnEmail: Maintainer email
  60. ## version: package version
  61. ## licenses: files: This specifies the files that the `license` covers,
  62. ## for example, it might be ``lib/*`` to cover the whole ``lib`` dir
  63. ## license: This specifies the license, for example gpl2, or lgpl.
  64. result = ""
  65. addN("Maintainer name: " & mtnName)
  66. addN("Email-Address: " & mtnEmail)
  67. addN("Date: " & $getTime())
  68. addN("Package Name: " & pkgName)
  69. addN("Version: " & version)
  70. for f, license in items(licenses):
  71. addN("Files: " & f)
  72. addN("License: " & license)
  73. proc formatDateTime(t: DateTime, timezone: string): string =
  74. var day = ($t.weekday)[0..2] & ", "
  75. return "$1$2 $3 $4 $5:$6:$7 $8" % [day, intToStr(t.monthday, 2),
  76. ($t.month)[0..2], $t.year, intToStr(t.hour, 2), intToStr(t.minute, 2),
  77. intToStr(t.second, 2), timezone]
  78. proc createChangelog(pkgName, version, maintainer: string): string =
  79. ## pkgName: package name
  80. ## version: package version
  81. ## maintainer: firstName lastName <email>
  82. result = ""
  83. addN(pkgName & " (" & version & "-1) unstable; urgency=low")
  84. addN("")
  85. addN(" * Initial release.")
  86. addN("")
  87. addN(" -- " & maintainer & " " &
  88. formatDateTime(utc(getTime()), "+0000"))
  89. proc createRules(): string =
  90. ## Creates a nim application-agnostic rules file for building deb packages.
  91. ## Please note: this assumes the c sources have been built and the
  92. ## ``build.sh`` and ``install.sh`` files are available.
  93. result = ""
  94. addN("#!/usr/bin/make -f")
  95. addN("%:")
  96. addN("\tdh $@\n")
  97. addN("dh_install:")
  98. addN("\tdh_install --sourcedir=debian/tmp")
  99. addN("override_dh_auto_clean:")
  100. addN("\tfind . -name *.o -exec rm {} \\;")
  101. addN("override_dh_auto_build:")
  102. addN("\t./build.sh")
  103. addN("override_dh_auto_install:")
  104. addN("\t./install.sh debian/tmp")
  105. proc createIncludeBinaries(binaries: seq[string]): string =
  106. return join(binaries, "\n")
  107. proc createDotInstall(pkgName: string, binaries, config, docs,
  108. lib: seq[string]): string =
  109. result = ""
  110. for b in binaries:
  111. addN(pkgName / b & " " & "usr/bin/")
  112. for c in config:
  113. addN(pkgName / c & " " & "etc/")
  114. for d in docs:
  115. addN(pkgName / d & " " & "usr/share/doc/nim/")
  116. for l1 in lib:
  117. addN(pkgName / l1 & " " & "usr/lib/nim")
  118. proc makeMtn(name, email: string): string =
  119. return name & " <" & email & ">"
  120. proc assertSuccess(exitCode: int) =
  121. doAssert(exitCode == QuitSuccess)
  122. proc prepDeb*(packName, version, mtnName, mtnEmail, shortDesc, desc: string,
  123. licenses: seq[tuple[files, license: string]], binaries,
  124. config, docs, lib: seq[string],
  125. buildDepends, pkgDepends = "") =
  126. ## binaries/config/docs/lib: files relative to nim's root, that need to
  127. ## be installed.
  128. let pkgName = packName.toLowerAscii()
  129. var workingDir = getTempDir() / "niminst" / "deb"
  130. var upstreamSource = (pkgName & "-" & version)
  131. echo("Making sure build.sh and install.sh are +x")
  132. assertSuccess execCmd("chmod +x \"" &
  133. (workingDir / upstreamSource / "build.sh") & "\"")
  134. assertSuccess execCmd("chmod +x \"" &
  135. (workingDir / upstreamSource / "install.sh") & "\"")
  136. var tarCmd = "tar pczf \"" &
  137. (pkgName & "_" & version & ".orig.tar.gz") &
  138. "\" \"" & upstreamSource & "\""
  139. echo(tarCmd)
  140. assertSuccess execCmd("cd \"" & workingDir & "\" && " & tarCmd)
  141. echo("Creating necessary files in debian/")
  142. createDir(workingDir / upstreamSource / "debian")
  143. template writeDebian(f, s: string) =
  144. writeFile(workingDir / upstreamSource / "debian" / f, s)
  145. var controlFile = createControl(pkgName, makeMtn(mtnName, mtnEmail),
  146. shortDesc, desc, buildDepends, pkgDepends)
  147. echo("debian/control")
  148. writeDebian("control", controlFile)
  149. var copyrightFile = createCopyright(pkgName, mtnName, mtnEmail, version,
  150. licenses)
  151. echo("debian/copyright")
  152. writeDebian("copyright", copyrightFile)
  153. var changelogFile = createChangelog(pkgName, version,
  154. makeMtn(mtnName, mtnEmail))
  155. echo("debian/changelog")
  156. writeDebian("changelog", changelogFile)
  157. echo("debian/rules")
  158. writeDebian("rules", createRules())
  159. echo("debian/compat")
  160. writeDebian("compat", "8")
  161. echo("debian/" & pkgName & ".install")
  162. writeDebian(pkgName & ".install",
  163. createDotInstall(pkgName, binaries, config, docs, lib))
  164. # Other things..
  165. createDir(workingDir / upstreamSource / "debian" / "source")
  166. echo("debian/source/format")
  167. writeDebian("source" / "format",
  168. "3.0 (quilt)")
  169. echo("debian/source/include-binaries")
  170. writeFile(workingDir / upstreamSource / "debian" / "source" / "include-binaries",
  171. createIncludeBinaries(binaries))
  172. echo("All done, you can now build.")
  173. echo("Before you do however, make sure the files in " &
  174. workingDir / upstreamSource / "debian" & " are correct.")
  175. echo("Change your directory to: " & workingDir / upstreamSource)
  176. echo("And execute `debuild -us -uc` to build the .deb")
  177. when isMainModule:
  178. #var controlFile = createControl("nim", "Dominik Picheta <morfeusz8@gmail.com>",
  179. # "The Nim compiler", "Compiler for the Nim programming language", "gcc (>= 4:4.3.2)", "gcc (>= 4:4.3.2)")
  180. #echo(controlFile)
  181. #var copyrightFile = createCopyright("nim", "Dominik Picheta", "morfeusz8@a.b", "0.8.14",
  182. # @[("bin/nim", "gpl2"), ("lib/*", "lgpl")])
  183. #echo copyrightFile
  184. #var changelogFile = createChangelog("nim", "0.8.14", "Dom P <m@b.c>")
  185. #echo(changelogFile)
  186. #echo(createRules())
  187. prepDeb("nim", "0.9.2", "Dominik Picheta", "morfeusz8@gmail.com",
  188. "The Nim compiler", "Compiler for the Nim programming language",
  189. @[("bin/nim", "MIT"), ("lib/*", "MIT")],
  190. @["bin/nim"], @["config/*"], @["doc/*"], @["lib/*"],
  191. "gcc (>= 4:4.3.2)", "gcc (>= 4:4.3.2)")