www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile time filesystem access?

reply Manu <turkeyman gmail.com> writes:
Is D capable of accessing the filesystem at compile time, for instance, to
load and parse an XML DOM, or some other structural metadata, which may be
used to generate the associative struct and its members?
I can think of many other uses for the technology too. It seems extremely
powerful, and I'm sure it's been discussed :)
Jan 30 2012
next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
 Is D capable of accessing the filesystem at compile time, for 
 instance, to
 load and parse an XML DOM, or some other structural metadata, 
 which may be
 used to generate the associative struct and its members?
 I can think of many other uses for the technology too. It seems 
 extremely
 powerful, and I'm sure it's been discussed :)
That's what the import("filename") expression is for: http://dlang.org/expression.html#ImportExpression You'll need to approve paths for inclusion with the -J switch.
Jan 30 2012
next sibling parent reply Manu <turkeyman gmail.com> writes:
On 30 January 2012 15:30, Vladimir Panteleev <vladimir thecybershadow.net>wrote:

 On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:

 Is D capable of accessing the filesystem at compile time, for instance, to
 load and parse an XML DOM, or some other structural metadata, which may be
 used to generate the associative struct and its members?
 I can think of many other uses for the technology too. It seems extremely
 powerful, and I'm sure it's been discussed :)
That's what the import("filename") expression is for: http://dlang.org/expression.**html#ImportExpression<http://dlang.org/expression.html#ImportExpression> You'll need to approve paths for inclusion with the -J switch.
Magic! :)
Jan 30 2012
parent David Nadlinger <see klickverbot.at> writes:
Be careful, though, the import expression (understandably) expects a 
compile-time string, which means that you can't directly use it from 
CTFE (e.g. to open additional files included by the IDL file you are 
parsing), but instead have to go back to compile-time land and then pass 
the contents back to CTFE in convoluted ways…

David



On 1/30/12 2:41 PM, Manu wrote:
 On 30 January 2012 15:30, Vladimir Panteleev
 <vladimir thecybershadow.net <mailto:vladimir thecybershadow.net>> wrote:

     On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:

         Is D capable of accessing the filesystem at compile time, for
         instance, to
         load and parse an XML DOM, or some other structural metadata,
         which may be
         used to generate the associative struct and its members?
         I can think of many other uses for the technology too. It seems
         extremely
         powerful, and I'm sure it's been discussed :)


     That's what the import("filename") expression is for:
     http://dlang.org/expression.__html#ImportExpression
     <http://dlang.org/expression.html#ImportExpression>

     You'll need to approve paths for inclusion with the -J switch.


 Magic! :)
Jan 30 2012
prev sibling next sibling parent reply Manu <turkeyman gmail.com> writes:
On 30 January 2012 15:41, Manu <turkeyman gmail.com> wrote:

 On 30 January 2012 15:30, Vladimir Panteleev
<vladimir thecybershadow.net>wrote:

 On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:

 Is D capable of accessing the filesystem at compile time, for instance,
 to
 load and parse an XML DOM, or some other structural metadata, which may
 be
 used to generate the associative struct and its members?
 I can think of many other uses for the technology too. It seems extremely
 powerful, and I'm sure it's been discussed :)
That's what the import("filename") expression is for: http://dlang.org/expression.**html#ImportExpression<http://dlang.org/expression.html#ImportExpression> You'll need to approve paths for inclusion with the -J switch.
Magic! :)
Here's another one I'm endlessly wishing I had in C. I want to know if a library is present, and automatically disable non-vital features if it isn't. It shits me to tears when I can't build something because a non-vital dependant lib is not available for a given platform or just not wanted.
Jan 30 2012
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
 I want to know if a library is present, and automatically 
 disable non-vital features if it isn't.
I'd like it too... here's what I tried. It doesn't work, though. === void libraryTest(string lib)() { mixin("import " ~ lib ~ ";"); } template libraryExists(string lib) { enum libraryExists = __traits(compiles, libraryTest!lib()); } void main() { static if(libraryExists!"arsd.cgi") { import arsd.cgi; auto cgi = new Cgi(); cgi.write("hello w/ cgi"); } else { import std.stdio; writeln("hello w/ stdio"); } } === That's the idea, but I couldn't get it to actually work. Imports are apparently done by the compiler before anything else (which makes sense, really) and that breaks the if compiles thing; it fails before it gets there. If you move the libraryTest inline to the compiles thing, it never uses the lib, so that's no good either. Alas.
Jan 30 2012
next sibling parent reply bls <bizprac orange.fr> writes:
On 01/30/2012 07:18 AM, Adam D. Ruppe wrote:
 On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
 I want to know if a library is present, and automatically disable
 non-vital features if it isn't.
I'd like it too... here's what I tried. It doesn't work, though.
I am afraid I miss the point but shouldn't module ctors help.. f.i. module m; import std.file; static this() // module ctor { if ("/usr/share/include/my.lib".isDir) import my; else import other; } Not tested!
Jan 30 2012
parent Manu <turkeyman gmail.com> writes:
On 30 January 2012 18:11, bls <bizprac orange.fr> wrote:

 On 01/30/2012 07:18 AM, Adam D. Ruppe wrote:

 On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:

 I want to know if a library is present, and automatically disable
 non-vital features if it isn't.
I'd like it too... here's what I tried. It doesn't work, though.
I am afraid I miss the point but shouldn't module ctors help.. f.i. module m; import std.file; static this() // module ctor { if ("/usr/share/include/my.lib".**isDir) import my; else import other; } Not tested!
That looks like a runtime test, and would still require both libs being present to link. I'm talking about detection at compile time. For example, I have a sound API here atm, the it supports mp3, vorbis, flac, wma, etc, but not all of those libraries are available for all platforms I build for. What I need to do is know in advance what platforms have which libs available, and then do a bunch of platform checks to decide which should or shouldn't be present. Further more, if a lib is *available* for a given platform, but I don't want to use it, or don't want to install/build the lib, I need to edit the code... If I could detect presence of the lib, I can just gracefully remove the feature.
Jan 30 2012
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 01/30/2012 04:18 PM, Adam D. Ruppe wrote:
 On Monday, 30 January 2012 at 14:24:32 UTC, Manu wrote:
 I want to know if a library is present, and automatically disable
 non-vital features if it isn't.
I'd like it too... here's what I tried. It doesn't work, though. === void libraryTest(string lib)() { mixin("import " ~ lib ~ ";"); } template libraryExists(string lib) { enum libraryExists = __traits(compiles, libraryTest!lib()); } void main() { static if(libraryExists!"arsd.cgi") { import arsd.cgi; auto cgi = new Cgi(); cgi.write("hello w/ cgi"); } else { import std.stdio; writeln("hello w/ stdio"); } } === That's the idea, but I couldn't get it to actually work. Imports are apparently done by the compiler before anything else (which makes sense, really) and that breaks the if compiles thing; it fails before it gets there. If you move the libraryTest inline to the compiles thing, it never uses the lib, so that's no good either. Alas.
The fact that this does not work is a compiler bug: static if(is(typeof({import asdf;}))){} it fails with exit code 1 without spitting out an error message. Once that is fixed, it could be used to detect presence or absence of a library. http://d.puremagic.com/issues/show_bug.cgi?id=7399
Jan 30 2012
prev sibling next sibling parent David Nadlinger <see klickverbot.at> writes:
On 1/30/12 3:24 PM, Manu wrote:
 Here's another one I'm endlessly wishing I had in C.
 I want to know if a library is present, and automatically disable
 non-vital features if it isn't.
 It shits me to tears when I can't build something because a non-vital
 dependant lib is not available for a given platform or just not wanted.
This could be an option, if it worked: http://d.puremagic.com/issues/show_bug.cgi?id=7399 David
Jan 30 2012
prev sibling parent reply "Marco Leise" <Marco.Leise gmx.de> writes:
Am 30.01.2012, 15:24 Uhr, schrieb Manu <turkeyman gmail.com>:

 Here's another one I'm endlessly wishing I had in C.
 I want to know if a library is present, and automatically disable  
 non-vital
 features if it isn't.
 It shits me to tears when I can't build something because a non-vital
 dependant lib is not available for a given platform or just not wanted.
A library is some .dll/.so in this case ? If you know it exists only on a certain platform: use version(Platform) If it _may_ be there, then compile the feature in, but load and check the library at runtime like you would do with a plugin. This sounds like what has been solved with configure scripts long ago. If you ever built e.g. GDC or most other Linux programs, they come with a little script that allows you to preconfigure your build process and takes care of linking to the different available libraries. ./configure --disable-libDontUseMe I never wrote one myself though...
Jan 30 2012
next sibling parent reply Manu <turkeyman gmail.com> writes:
On 30 January 2012 19:30, Marco Leise <Marco.Leise gmx.de> wrote:

 Am 30.01.2012, 15:24 Uhr, schrieb Manu <turkeyman gmail.com>:

  Here's another one I'm endlessly wishing I had in C.
 I want to know if a library is present, and automatically disable
 non-vital
 features if it isn't.
 It shits me to tears when I can't build something because a non-vital
 dependant lib is not available for a given platform or just not wanted.
A library is some .dll/.so in this case ? If you know it exists only on a certain platform: use version(Platform) If it _may_ be there, then compile the feature in, but load and check the library at runtime like you would do with a plugin.
I'm talking about static libs, since you need to link the supporting code for DLL's anyway. This sounds like what has been solved with configure scripts long ago. If
 you ever built e.g. GDC or most other Linux programs, they come with a
 little script that allows you to preconfigure your build process and takes
 care of linking to the different available libraries.
Configure scripts certainly don't 'solve' the problem, they make it worse... now my buildscript has twice as many steps, nobody can understand it, and it only works on linux. ./configure --disable-libDontUseMe
 I never wrote one myself though...
Exactly, and nobody I've ever met has either. They just seem to exist, magically appeared out of nowhere in all major linux projects that have existed for 20 years or so. As far as I can tell, nobody ACTUALLY wrote them, they've just always been there... ;)
Jan 30 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-01-30 18:48, Manu wrote:
 On 30 January 2012 19:30, Marco Leise <Marco.Leise gmx.de
 <mailto:Marco.Leise gmx.de>> wrote:

     Am 30.01.2012, 15:24 Uhr, schrieb Manu <turkeyman gmail.com
     <mailto:turkeyman gmail.com>>:

         Here's another one I'm endlessly wishing I had in C.
         I want to know if a library is present, and automatically
         disable non-vital
         features if it isn't.
         It shits me to tears when I can't build something because a
         non-vital
         dependant lib is not available for a given platform or just not
         wanted.


     A library is some .dll/.so in this case ?
     If you know it exists only on a certain platform: use version(Platform)
     If it _may_ be there, then compile the feature in, but load and
     check the library at runtime like you would do with a plugin.


 I'm talking about static libs, since you need to link the supporting
 code for DLL's anyway.

     This sounds like what has been solved with configure scripts long
     ago. If you ever built e.g. GDC or most other Linux programs, they
     come with a little script that allows you to preconfigure your build
     process and takes care of linking to the different available libraries.


 Configure scripts certainly don't 'solve' the problem, they make it
 worse... now my buildscript has twice as many steps, nobody can
 understand it, and it only works on linux.

             ./configure --disable-libDontUseMe

     I never wrote one myself though...


 Exactly, and nobody I've ever met has either. They just seem to exist,
 magically appeared out of nowhere in all major linux projects that have
 existed for 20 years or so. As far as I can tell, nobody ACTUALLY wrote
 them, they've just always been there... ;)
Aren't those configure scripts generate by some other tool :) -- /Jacob Carlborg
Jan 30 2012
parent reply "Marco Leise" <Marco.Leise gmx.de> writes:
 Aren't those configure scripts generate by some other tool :)
Maybe. I never digged that deep into build processes. After some time I got myself to write Makefiles, but that's about it. I see names like automake, m4, configure etc. pop up now and then and have no idea what role each plays. They just get the job done somehow and check which C standard library is used, what compiler and other useful traits to cover the differences between systems.
Jan 30 2012
parent Danni Coy <danni.coy gmail.com> writes:
configure scripts are usually part of the automake build system which used
to be the standard on linux maybe 5 years ago I think it went out of favour
roughly the same time as cvs.

On Tue, Jan 31, 2012 at 5:29 AM, Marco Leise <Marco.Leise gmx.de> wrote:

 Aren't those configure scripts generate by some other tool :)

 Maybe. I never digged that deep into build processes. After some time I
 got myself to write Makefiles, but that's about it. I see names like
 automake, m4, configure etc. pop up now and then and have no idea what role
 each plays. They just get the job done somehow and check which C standard
 library is used, what compiler and other useful traits to cover the
 differences between systems.
Jan 30 2012
prev sibling next sibling parent Iain Buclaw <ibuclaw ubuntu.com> writes:
On 30 January 2012 17:48, Manu <turkeyman gmail.com> wrote:
 On 30 January 2012 19:30, Marco Leise <Marco.Leise gmx.de> wrote:
 Am 30.01.2012, 15:24 Uhr, schrieb Manu <turkeyman gmail.com>:

 Here's another one I'm endlessly wishing I had in C.
 I want to know if a library is present, and automatically disable
 non-vital
 features if it isn't.
 It shits me to tears when I can't build something because a non-vital
 dependant lib is not available for a given platform or just not wanted.
A library is some .dll/.so in this case ? If you know it exists only on a certain platform: use version(Platform) If it _may_ be there, then compile the feature in, but load and check th=
e
 library at runtime like you would do with a plugin.
I'm talking about static libs, since you need to link the supporting code for DLL's anyway.
 This sounds like what has been solved with configure scripts long ago. I=
f
 you ever built e.g. GDC or most other Linux programs, they come with a
 little script that allows you to preconfigure your build process and tak=
es
 care of linking to the different available libraries.
Configure scripts certainly don't 'solve' the problem, they make it worse=
...
 now my buildscript has twice as many steps, nobody can understand it, and=
it
 only works on linux.

 =A0 =A0 =A0 =A0./configure --disable-libDontUseMe

 I never wrote one myself though...
Exactly, and nobody I've ever met has either. They just seem to exist, magically appeared out of nowhere in all major linux projects that have existed for 20 years or so. As far as I can tell, nobody ACTUALLY wrote them, they've just always been there... ;)
Much like my book about the paranormal... I didn't buy, it just appeared in my room one night. --=20 Iain Buclaw *(p < e ? p++ : p) =3D (c & 0x0f) + '0';
Jan 30 2012
prev sibling parent Manu <turkeyman gmail.com> writes:
On 30 January 2012 20:03, Iain Buclaw <ibuclaw ubuntu.com> wrote:

 On 30 January 2012 17:48, Manu <turkeyman gmail.com> wrote:
 Exactly, and nobody I've ever met has either. They just seem to exist,
 magically appeared out of nowhere in all major linux projects that have
 existed for 20 years or so. As far as I can tell, nobody ACTUALLY wrote
 them, they've just always been there... ;)
Much like my book about the paranormal... I didn't buy, it just appeared in my room one night.
Precisely! :P
Jan 30 2012
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Hear hear! How I understand what you mean!
"I just wanna freakin' look at it, but build a legitimate magical
mystery toy with endless possibilities!"

On Mon, Jan 30, 2012 at 6:24 PM, Manu <turkeyman gmail.com> wrote:
 On 30 January 2012 15:41, Manu <turkeyman gmail.com> wrote:
 On 30 January 2012 15:30, Vladimir Panteleev <vladimir thecybershadow.net>
 wrote:
 On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
 Is D capable of accessing the filesystem at compile time, for instance,
 to
 load and parse an XML DOM, or some other structural metadata, which may
 be
 used to generate the associative struct and its members?
 I can think of many other uses for the technology too. It seems
 extremely
 powerful, and I'm sure it's been discussed :)
That's what the import("filename") expression is for: http://dlang.org/expression.html#ImportExpression You'll need to approve paths for inclusion with the -J switch.
Magic! :)
Here's another one I'm endlessly wishing I had in C. I want to know if a library is present, and automatically disable non-vital features if it isn't. It shits me to tears when I can't build something because a non-vital dependant lib is not available for a given platform or just not wanted.
-- Bye, Gor Gyolchanyan.
Jan 30 2012
prev sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Monday, 30 January 2012 at 13:23:19 UTC, Manu wrote:
 Is D capable of accessing the filesystem at compile time, for 
 instance, to
 load and parse an XML DOM, or some other structural metadata, 
 which may be
 used to generate the associative struct and its members?
 I can think of many other uses for the technology too. It seems 
 extremely
 powerful, and I'm sure it's been discussed :)
As interesting as that would be, I wouldn't recommend it. Just generate a .d file from the XML before compiling. It will be faster, and more flexible than trying to hit everything with the D hammer.
Jan 30 2012