From f7114ca7884d75d1ffc0f2c6e07d8c9a904ebab2 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sat, 11 Apr 2015 21:35:10 +0100 Subject: [PATCH] Implemented ``nimble c``. --- readme.markdown | 18 ++++++++++--- src/nimble.nim | 68 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/readme.markdown b/readme.markdown index 50619fd..4f59a70 100644 --- a/readme.markdown +++ b/readme.markdown @@ -29,7 +29,7 @@ the following commands to clone nimble, compile it and then install it. git clone https://github.com/nim-lang/nimble.git cd nimble nim c -r src/nimble install - + After these steps nimble should be compiled and installed. You should then add ``~/.nimble/bin`` to your ``$PATH``. Updating nimble can then be done by executing ``nimble install nimble``. @@ -118,7 +118,7 @@ Example: Nimble always fetches and installs the latest version of a package. Note that latest version is defined as the latest tagged version in the git (or hg) repository, if the package has no tagged versions then the latest commit in the -remote repository will be installed. If you already have that version installed +remote repository will be installed. If you already have that version installed Nimble will ask you whether you wish it to overwrite your local copy. You can force Nimble to download the latest commit from the package's repo, for @@ -139,7 +139,7 @@ In this case a version which is greater than ``0.5`` will be installed. If you don't specify a parameter and there is a ``package.nimble`` file in your current working directory then Nimble will install the package residing in the current working directory. This can be useful for developers who are testing -locally their ``.nimble`` files before submitting them to the official package +locally their ``.nimble`` files before submitting them to the official package list. See [developers.markdown](developers.markdown) for more info on this. A URL to a repository can also be specified, Nimble will automatically detect @@ -161,6 +161,16 @@ The ``build`` command is mostly used by developers who want to test building their ``.nimble`` package. The ``install`` command calls ``build`` implicitly, so there is rarely any reason to use this command directly. +### nimble c + +The ``c`` (or ``compile``, ``js``, ``cc``, ``cpp``) command can be used by +developers to compile individual modules inside their package. All options +passed to Nimble will also be passed to the Nim compiler during compilation. + +Nimble will use the backend specified in the package's ``.nimble`` file if +the command ``c`` or ``compile`` is specified. The more specific ``js``, ``cc``, +``cpp`` can be used to override that. + ### nimble list The ``list`` command will display the known list of packages available for @@ -183,7 +193,7 @@ substrings). Example: tags: library, opengl, math, game description: OpenGL math library license: CC0 - + extmath: url: git://github.com/achesak/extmath.nim (git) tags: library, math, trigonometry diff --git a/src/nimble.nim b/src/nimble.nim index f57af3a..b33f03d 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -23,7 +23,7 @@ type ActionType = enum actionNil, actionUpdate, actionInit, actionInstall, actionSearch, - actionList, actionBuild, actionPath, actionUninstall + actionList, actionBuild, actionPath, actionUninstall, actionCompile Action = object case typ: ActionType @@ -38,7 +38,11 @@ type search: seq[string] # Search string. of actionInit: projName: string - else:nil + of actionCompile: + file: string + backend: string + compileOptions: seq[string] + else: nil ForcePrompt = enum dontForcePrompt, forcePromptYes, forcePromptNo @@ -52,6 +56,8 @@ Commands: init [pkgname] Initializes a new Nimble project. uninstall [pkgname, ...] Uninstalls a list of packages. build Builds a package. + c, cc, js [opts, ...] f.nim Builds a file inside a package. Passes options + to the Nim compiler. update [url] Updates package list. A package list URL can be optionally specified. search [--ver] pkg/tag Searches for a specified package. Search is @@ -148,6 +154,12 @@ proc parseCmdLine(): Options = result.action.packages = @[] of "build": result.action.typ = actionBuild + of "c", "compile", "js", "cpp", "cc": + result.action.typ = actionCompile + result.action.compileOptions = @[] + result.action.file = "" + if key == "c" or key == "compile": result.action.backend = "" + else: result.action.backend = key of "init": result.action.typ = actionInit result.action.projName = "" @@ -185,19 +197,28 @@ proc parseCmdLine(): Options = raise newException(NimbleError, "Can only initialize one package at a time.") result.action.projName = key + of actionCompile: + result.action.file = key of actionList, actionBuild: writeHelp() else: discard of cmdLongOption, cmdShortOption: - case key - of "help", "h": writeHelp() - of "version", "v": writeVersion() - of "accept", "y": result.forcePrompts = forcePromptYes - of "reject", "n": result.forcePrompts = forcePromptNo - of "ver": result.queryVersions = true - of "installed", "i": result.queryInstalled = true - else: discard + case result.action.typ + of actionCompile: + if val == "": + result.action.compileOptions.add("--" & key) + else: + result.action.compileOptions.add("--" & key & ":" & val) + else: + case key + of "help", "h": writeHelp() + of "version", "v": writeVersion() + of "accept", "y": result.forcePrompts = forcePromptYes + of "reject", "n": result.forcePrompts = forcePromptNo + of "ver": result.queryVersions = true + of "installed", "i": result.queryInstalled = true + else: discard of cmdEnd: assert(false) # cannot happen if result.action.typ == actionNil: writeHelp() @@ -640,6 +661,31 @@ proc build(options: Options) = let paths = processDeps(pkginfo, options) buildFromDir(pkgInfo, paths) +proc compile(options: Options) = + var pkgInfo = getPkgInfo(getCurrentDir()) + let paths = processDeps(pkginfo, options) + let realDir = pkgInfo.getRealDir() + + var args = "" + for path in paths: args.add("--path:\"" & path & "\" ") + for option in options.action.compileOptions: + args.add(option & " ") + + let bin = options.action.file + let backend = + if options.action.backend.len > 0: + options.action.backend + else: + pkgInfo.backend + + if bin == "": + raise newException(NimbleError, "You need to specify a file to compile.") + + echo("Compiling ", bin, " (", pkgInfo.name, ") using ", backend, + " backend...") + doCmd(getNimBin() & " $# --noBabelPath $# \"$#\"" % + [backend, args, bin]) + proc search(options: Options) = ## Searches for matches in ``options.action.search``. ## @@ -855,6 +901,8 @@ proc doAction(options: Options) = listPaths(options) of actionBuild: build(options) + of actionCompile: + compile(options) of actionInit: init(options) of actionNil: