debcreation.nim 7.8 KB

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