incremental.nim 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Basic type definitions the module graph needs in order to support
  10. ## incremental compilations.
  11. const nimIncremental* = defined(nimIncremental)
  12. import options, lineinfos
  13. when nimIncremental:
  14. import ast, msgs, intsets, btrees, db_sqlite, std / sha1, pathutils
  15. from strutils import parseInt
  16. from os import isAbsolute
  17. type
  18. Writer* = object
  19. sstack*: seq[PSym] # a stack of symbols to process
  20. tstack*: seq[PType] # a stack of types to process
  21. tmarks*, smarks*: IntSet
  22. forwardedSyms*: seq[PSym]
  23. Reader* = object
  24. syms*: BTree[int, PSym]
  25. types*: BTree[int, PType]
  26. IncrementalCtx* = object
  27. db*: DbConn
  28. w*: Writer
  29. r*: Reader
  30. configChanged*: bool
  31. proc init*(incr: var IncrementalCtx) =
  32. incr.w.sstack = @[]
  33. incr.w.tstack = @[]
  34. incr.w.tmarks = initIntSet()
  35. incr.w.smarks = initIntSet()
  36. incr.w.forwardedSyms = @[]
  37. incr.r.syms = initBTree[int, PSym]()
  38. incr.r.types = initBTree[int, PType]()
  39. proc hashFileCached*(conf: ConfigRef; fileIdx: FileIndex; fullpath: AbsoluteFile): string =
  40. result = msgs.getHash(conf, fileIdx)
  41. if result.len == 0 and isAbsolute(string fullpath):
  42. result = $secureHashFile(string fullpath)
  43. msgs.setHash(conf, fileIdx, result)
  44. proc toDbFileId*(incr: var IncrementalCtx; conf: ConfigRef; fileIdx: FileIndex): int =
  45. if fileIdx == FileIndex(-1): return -1
  46. let fullpath = toFullPath(conf, fileIdx)
  47. let row = incr.db.getRow(sql"select id, fullhash from filenames where fullpath = ?",
  48. fullpath)
  49. let id = row[0]
  50. let fullhash = hashFileCached(conf, fileIdx, AbsoluteFile fullpath)
  51. if id.len == 0:
  52. result = int incr.db.insertID(sql"insert into filenames(nimid, fullpath, fullhash) values (?, ?, ?)",
  53. int(fileIdx), fullpath, fullhash)
  54. else:
  55. if row[1] != fullhash:
  56. incr.db.exec(sql"update filenames set fullhash = ? where fullpath = ?", fullhash, fullpath)
  57. result = parseInt(id)
  58. proc fromDbFileId*(incr: var IncrementalCtx; conf: ConfigRef; dbId: int): FileIndex =
  59. if dbId == -1: return FileIndex(-1)
  60. let fullpath = incr.db.getValue(sql"select fullpath from filenames where id = ?", dbId)
  61. doAssert fullpath.len > 0, "cannot find file name for DB ID " & $dbId
  62. result = fileInfoIdx(conf, AbsoluteFile fullpath)
  63. proc addModuleDep*(incr: var IncrementalCtx; conf: ConfigRef;
  64. module, fileIdx: FileIndex;
  65. isIncludeFile: bool) =
  66. if conf.symbolFiles != v2Sf: return
  67. let a = toDbFileId(incr, conf, module)
  68. let b = toDbFileId(incr, conf, fileIdx)
  69. incr.db.exec(sql"insert into deps(module, dependency, isIncludeFile) values (?, ?, ?)",
  70. a, b, ord(isIncludeFile))
  71. # --------------- Database model ---------------------------------------------
  72. proc createDb*(db: DbConn) =
  73. db.exec(sql"""
  74. create table if not exists controlblock(
  75. idgen integer not null
  76. );
  77. """)
  78. db.exec(sql"""
  79. create table if not exists config(
  80. config varchar(8000) not null
  81. );
  82. """)
  83. db.exec(sql"""
  84. create table if not exists filenames(
  85. id integer primary key,
  86. nimid integer not null,
  87. fullpath varchar(8000) not null,
  88. fullHash varchar(256) not null
  89. );
  90. """)
  91. db.exec sql"create index if not exists FilenameIx on filenames(fullpath);"
  92. db.exec(sql"""
  93. create table if not exists modules(
  94. id integer primary key,
  95. nimid integer not null,
  96. fullpath varchar(8000) not null,
  97. interfHash varchar(256) not null,
  98. fullHash varchar(256) not null,
  99. created timestamp not null default (DATETIME('now'))
  100. );""")
  101. db.exec(sql"""create unique index if not exists SymNameIx on modules(fullpath);""")
  102. db.exec(sql"""
  103. create table if not exists deps(
  104. id integer primary key,
  105. module integer not null,
  106. dependency integer not null,
  107. isIncludeFile integer not null,
  108. foreign key (module) references filenames(id),
  109. foreign key (dependency) references filenames(id)
  110. );""")
  111. db.exec(sql"""create index if not exists DepsIx on deps(module);""")
  112. db.exec(sql"""
  113. create table if not exists types(
  114. id integer primary key,
  115. nimid integer not null,
  116. module integer not null,
  117. data blob not null,
  118. foreign key (module) references module(id)
  119. );
  120. """)
  121. db.exec sql"create index TypeByModuleIdx on types(module);"
  122. db.exec sql"create index TypeByNimIdIdx on types(nimid);"
  123. db.exec(sql"""
  124. create table if not exists syms(
  125. id integer primary key,
  126. nimid integer not null,
  127. module integer not null,
  128. name varchar(256) not null,
  129. data blob not null,
  130. exported int not null,
  131. foreign key (module) references module(id)
  132. );
  133. """)
  134. db.exec sql"create index if not exists SymNameIx on syms(name);"
  135. db.exec sql"create index SymByNameAndModuleIdx on syms(name, module);"
  136. db.exec sql"create index SymByModuleIdx on syms(module);"
  137. db.exec sql"create index SymByNimIdIdx on syms(nimid);"
  138. db.exec(sql"""
  139. create table if not exists toplevelstmts(
  140. id integer primary key,
  141. position integer not null,
  142. module integer not null,
  143. data blob not null,
  144. foreign key (module) references module(id)
  145. );
  146. """)
  147. db.exec sql"create index TopLevelStmtByModuleIdx on toplevelstmts(module);"
  148. db.exec sql"create index TopLevelStmtByPositionIdx on toplevelstmts(position);"
  149. db.exec(sql"""
  150. create table if not exists statics(
  151. id integer primary key,
  152. module integer not null,
  153. data blob not null,
  154. foreign key (module) references module(id)
  155. );
  156. """)
  157. db.exec sql"create index StaticsByModuleIdx on toplevelstmts(module);"
  158. db.exec sql"insert into controlblock(idgen) values (0)"
  159. else:
  160. type
  161. IncrementalCtx* = object
  162. template init*(incr: IncrementalCtx) = discard
  163. template addModuleDep*(incr: var IncrementalCtx; conf: ConfigRef;
  164. module, fileIdx: FileIndex;
  165. isIncludeFile: bool) =
  166. discard