<div dir="ltr">Hi Mariano,<div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 16, 2016 at 6:25 PM, Mariano Martinez Peck <span dir="ltr">&lt;<a href="mailto:marianopeck@gmail.com" target="_blank">marianopeck@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Sat, Jan 16, 2016 at 11:02 PM, Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span>On Sat, Jan 16, 2016 at 6:00 AM, Mariano Martinez Peck <span dir="ltr">&lt;<a href="mailto:marianopeck@gmail.com" target="_blank">marianopeck@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><div dir="ltr">Hi all,<div><br></div><div>Sorry for reviving an old thread but I thought it was better to continue the discussion here because of the context. </div><div>As you may have read, the other day I released a first approeach to a subset of OSProcess based on FFI (posix_spwan() family of functions):</div><div><br></div><div><a href="https://github.com/marianopeck/OSSubprocess" target="_blank">https://github.com/marianopeck/OSSubprocess</a><br></div><div><br></div><div> And with that in mind, I wanted to share a few things with you. The main 2 problems I found with implementing this with FFI was:</div><div><br></div><div>1) We have all already agree and discussed that fork+exec cannot be done in separate FFI calls. So at the very min you need either a plugin method that does the fork()+exec() OR wrapping a lib like posix_spwan()</div><div><br></div><div>2) The other main problem, is, as you all said (and mostly  Nicolas), is the problems with the preprocessor (constants, macros, etc).</div><div><br></div><div>With all that said, I was able to get my stuff working. However, I am still using some primitives of OSProcess plugin because of 2). </div><div><br></div><div>I read Eliot idea and what I don&#39;t like is the need of a C compiler in the user machine. I think that&#39;s a high constrain. Then Igor suggested that WE (developers and maintainers of a certain tool) are the ones that compiles the little C program to extract constant values etc and then WE provide as part of our source code, some packages with some SharedPool depending on the platform/OS. And Igor approach looked a bit better to me.</div></div></blockquote><div><br></div><div style="color:rgb(0,0,0);font-size:12.8px"><br><br></div></span><div style="color:rgb(0,0,0);font-size:12.8px">You misunderstand the proposal. </div></div></div></div></blockquote><div><br></div></span><div>I think I did. But let me confirm that below ;)</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div style="color:rgb(0,0,0);font-size:12.8px"> The C compiler is needed /only when changing the set of constants/, i.e. when /developing/ the interface.  The C compiler is /not/ needed when deploying.</div><div style="color:rgb(0,0,0);font-size:12.8px"><br></div><div style="color:rgb(0,0,0);font-size:12.8px">The idea is to</div><div style="color:rgb(0,0,0);font-size:12.8px">a) at development time, e.g. when a new variable is added to a SharedPool containing platform constants, a C program is autogenerated that outputs in some format a description of the names and values of all the constants defined in the pool.  One convenient notation is e.g. STON.  For the purposes of this discussion let&#39;s assume we&#39;re using ston, but any format the image an parse (or indeed a shared object the image can load on teh current pkatform) will do.  The output of the autogenerated C program would be called something like &lt;SharedPoolName&gt;.&lt;PlatformName&gt;.ston, e.g. UnixConstants.MacOSX64.ston or UnixConstants.Linux32.ston.  The ston files can easily be parsed by facilities in the Smalltalk image.</div><div style="color:rgb(0,0,0);font-size:12.8px"><br></div><div style="color:rgb(0,0,0);font-size:12.8px">b) when deploying the system to a set of platforms one includes all the relevant platform-specific ston files.</div><div style="color:rgb(0,0,0);font-size:12.8px"><br></div></div></div></div></blockquote><div><br></div></span><div>OK. But let me ask something. Below you said &quot;<font color="#000000"><span style="font-size:12.8px">be it a plugin or a dll doesn&#39;t matter&quot;. To autogenerate the C program, I must know which header files to include for each platform and probably a few others things. For example, besides exporting the value,  I would also like to export the sizeof(). At that depends how was the VM compiled, right?   So...my question is...if such a autogenerated C code could be part of the VM building (considering all the settings being assume when building), cannot I reuse the knowledge the VM already has? Like which header files to include, if it was compiled 32 bits or 64 bits, which C compiler to use, etc..</span></font></div></div></div></div></blockquote><div><br></div><div>I actually said that using text is easier than a dll.  So I&#39;m saying  autogenerate a C program that outputs name-value pairs in some convenient textual representation, e.g. ston.  But answering your question...</div><div><br></div><div>The knowledge in the VM as to what header files are included *applies only to the include files the VM uses*.  The VM uses a subset of the platform.  It doesn&#39;t for example include any headers that define a database interface.  It doesn&#39;t include header files that define the interface to a UI tooklit such at GTK.  Etc, etc.  So in fact the VM *doesn&#39;t* include the knowledge one needs to determine the set of include files for an arbitrary FFI interface.  And even so, the include files that it does use are in the VM&#39;s platform source files, and that information is not readily accessible.</div><div><br></div><div>Let me summarise.  No, the VM cannot be used to determine the set of include files needed to generate constants used in an arbitrary FFI interface.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><span style="font-size:12.8px;color:rgb(0,0,0)">What I mean is if it would be easier if I take the SharedPool at VM building time, and from there I autogenerate (and run) the C code that would generate the output. Then, when we &quot;deploy&quot; the VM, we can deploy it with relevant platform specific ston files as you said. </span><br></div></div></div></div></blockquote><div><br></div><div>No.  The VM is something that provides an FFI.  It doesn&#39;t *define* an FFI.   One must be able to develop an FFI interface without needing to rebuild the VM.  So computing the values of constants should be *separate* from building a VM.  Now let me give you more of an example.</div><div><br></div><div>Let&#39;s say we define a subclass of SharedPool called FFISharedPool.   FFISharedPool &#39;s job is to manage autogenerating a C file, compiling it for the platform, and organizing parsing the relevant output.  Let&#39;s say we use a convention like class-side pragmas to define include files, and compiler flags.  The VM provides two crucial pieces of information:</div><div><br></div><div>1. the platform name</div><div>2. the word size</div><div><br></div><div>One can&#39;t run a Mac OS VM on Linux, and one can&#39;t run a 64-bit VM on a 32-bit operating system.  So taking this information from the VM accurately tells the current system what ABI (application binary interface) to use, and that&#39;s what&#39;s important in generating the right constants. </div><div><br></div><div>So we use these two pieces of information to index the method pragmas that tell us what specific files to include.</div><div><br></div><div>Let&#39;s imagine we subclass FFISharedPool to add a shared pool for constants for an SQL database.  We might have a class declaration like</div><div><br></div><div><div>FFISharedPool subclass: #MYSQLInterface</div><div><span class="" style="white-space:pre">        </span>instanceVariableNames: &#39;&#39;</div><div><span class="" style="white-space:pre">        </span>classVariableNames: &#39;MYSQL_DEFAULT_AUTH MYSQL_ENABLE_CLEARTEXT_PLUGIN MYSQL_INIT_COMMAND MYSQL_OPT_BIND MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS MYSQL_OPT_COMPRESS MYSQL_OPT_CONNECT_ATTR_DELETE MYSQL_OPT_CONNECT_ATTR_RESET&#39;</div><div><span class="" style="white-space:pre">        </span>poolDictionaries: &#39;&#39;</div><div><span class="" style="white-space:pre">        </span>category: &#39;MYSQLInterface-Pools&#39;</div></div><div><br></div><div>The job of FFISharedPool is to compute the right values for the class variables on every platform we want to deploy the MYSQL interface on.</div><div><br></div><div>So we need to know the relevant include files and C flags for each platform/word-size combination.  A few of them might look like</div><div><br></div><div><br></div><div>MYSQLInterface class methods for platform information<br></div><div>mac32</div><div>    &quot;I describe the include files and C flags to use when developing a 32-bit MYSQL FFI interface on Mac OS X&quot;</div><div>    &lt;platformName: &#39;Mac OS&#39; wordSize: 4&gt;</div><div>    &lt;cFlags: #(&#39;-m32&#39;) includeFiles: #(&#39;/opt/mysql/include32&#39;)&gt;</div><div>    ^self &quot;all the info is in the pragmas&quot;</div><div><div><br class="">mac64</div><div>    &quot;I describe the include files and C flags to use when developing a 64-bit MYSQL FFI interface on Mac OS X&quot;</div><div>    &lt;platformName: &#39;Mac OS&#39; wordSize: 8&gt;</div><div>    &lt;cFlags: #(&#39;-m64&#39;) includeFiles: #(&#39;/opt/mysql/include64&#39;)&gt;</div></div><div><br></div><div>The above might cause FFISharedPool to autogenerate files called MYSQLInterface.mac32.c &amp; MYSQLInterface.mac64.c.  And these, when run, might output ston notation to MYSQLInterface.mac32.ston &amp; MYSQLInterface.mac64.ston (or maybe to stdout which has to be redirected to MYSQLInterface.mac32.ston; whatever).</div><div><br></div><div>Now, you might use pragmas, or you might answer a Dictionary instance.  What ever style pleases you and seems convenient and readable.  But these methods define the necessary metadata (C flags, include paths, and ...?) for FFISharedPool to autogenerate the C program that, when compiled with the supplied C flags and run on the current platform, outputs the values for the constants the shared pool wants to define.</div><div><br></div><div><br></div><div>You can get fancy and have FFISharedPool autogenerate the C programs whenever one adds or removes a constant name.  Or you can require the programmer run something, e.g. MYSQLInterface generateInterfaces.  It&#39;s really nice if FFISharedPool submits the file to the C compiler automatically, but this can only work for e.g. 32 &amp; 64 bit versions on a single platform.  You have to compile the autogenerated program on the relevant platform, with the necessary libraries and include files installed.</div><div><br></div><div>You could imagine a set of servers for different platforms so one could submit the autogenerated program for compilation and execution on each platform.  That&#39;s a facility I&#39;d make it easy to implement.  I could imagine that a programmer whose company develops an FFI interface and deploys it on a number of platforms would love to be able to automate compiling and running the relevant autogenerated code on a set of servers.  I could imagine the Pharo community providing a set of servers upon which lots of software is installed for precisely this purpose. That means that people could develop FFI interfaces without even having to have the C compiler installed on their platform.</div><div><br></div><div>You could also add a C parser to FFISharedPool  that parses the post-preprocessed code and extracts function declarations.  But the important thing is autogenerating the C program so that it generates easily parsable output containing the values for the constants.  You can extend the system in interesting ways once you ave this core functionality implemented.</div><div><br></div><div>So once the program is autogenerated and compiled for the current platform, it is run and its output collected in a file whose name can be recognised by FFISharedPool.</div><div><br></div><div><br></div><div>Now the class side of FFISharedPool might be declared as</div><div><br></div><div>FFIShardPool class</div><div><span class="" style="white-space:pre">        </span>instanceVariableNames: &#39;platformName wordSize&#39;<br></div><div><br></div><div>and on start-up FFIShardPool could examine its subclasses, and for each whose platformName &amp; wordSize do not match the current platform, search for all the matching FOOInterface.plat.ston files, parse them and update the subclasses&#39; variables, and update that pool&#39;s platformName &amp; wordSize.  It could emit a warning on the Transcript or stdout (headful vs headless) indicating which subclasses it couldn&#39;t find the relevant FOOInterface.plat.ston files for.</div><div><br></div><div>But the end result is that</div><div><br></div><div>a) providing the system is deployed with FOOInterface.plat.ston files for each interface and platform used, a cross-platform application can be deployed *that does not require a C compiler*.</div><div>b) providing that a system&#39;s FOOInterface files have been initialized on the intended platform, a platform-specific application can be deployed for a single platform *without needing the ston files*.</div><div><br></div><div>Does this make more sense now?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div style="color:rgb(0,0,0);font-size:12.8px"></div><div style="color:rgb(0,0,0);font-size:12.8px">c) at startup the image checks its current platform.  If the platform is the same that it was saved on, no action is taken.  But if the platform as changed then the relevant ston file is selected, parsed, and the values for the variables in the shared pool updated to reflect the values of the current platform.</div><div style="color:rgb(0,0,0);font-size:12.8px"><br></div><div style="color:rgb(0,0,0);font-size:12.8px">So the C compiler is only needed when developing the interface, not when deploying it.</div><span><div style="color:rgb(0,0,0);font-size:12.8px"><br></div></span></div></div></div></blockquote><div> </div></span><div>OK </div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><div style="color:rgb(0,0,0);font-size:12.8px"></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>Then Nicolas made a point that if we plan to manage all that complexity at the image level it may become a hell too. </div><div><br></div><div>So.... what if we take a simpler (probably not better) approach and we consider the &quot;c program that exports constants and sizes&quot; a VM Plugin? Let&#39;s say we have a UnixPreprocessorPlugin (that would work for OSX, Linux and other&#39;s Unix I imagine for the time being) which provides a function (that is exported) which answers an array of arrays. For each constant, we include the name of the constant, the value, and the sizeof().  Then from image side, we simply do one FFI call, we get the large array and we adapt it to a SharedPool or whatever kind of object representing that info. </div></div></blockquote><div><br></div><div style="color:rgb(0,0,0);font-size:12.8px"><br><br></div></span><div><span style="color:rgb(0,0,0);font-size:12.8px">This is what I suggestred in teh first place.  That what is autogenerated is a shared object (be it a plgin or a dll doesn&#39;t matter, it is machine code generated by a C compiler form an autogenerated C program compiled with the platform&#39;s C compiler) that can be loaded at run-time and interrogated to fetch the values of a set of variables</span></div></div></div></div></blockquote><div><br></div></span><div>OK, got it. But still, it would be easier if the &quot;platform&quot; in this case is the &quot;machine where we build the VM we will then distribute&quot; right? i mean, I would like to put this in the CI jobs that automatically builds the VM, and not myself building for each platform. </div></div></div></div></blockquote><div><br></div><div>NO!  For example, why would a company that has some proprietary arithmetic package implemented in its secret labs in C or C++ and accessed through the FFI want to have that code on the Pharo community&#39;s build servers?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div><b>I mean, my main doubt is if this job of autogenerating C code, compile it, run it, export text file, and distribute text file with the VM, could be done as part of the VM building. </b></div></div></div></div></blockquote><div><br></div><div>For fuck&#39;s sake.  Developing an FFI is not something one does when building a VM.  It is something one does wen using the system.  f you want to do this you *use a plugin*.  The FFI is a different beast.  It is to allow programers to interface to external librarys that are *independent from teh VM*.</div><div><br></div><div>I&#39;m not going to answer this one again.  OK?</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class=""><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><span style="color:rgb(0,0,0);font-size:12.8px">.  But I think that the textual notation suggested above is simpler.  The test files are easier to distribute and change.</span>  Shared objects and plugins have a habit of going stale, and there needs to be metadata in there to describe the set of constants etc, which is tricky to generate and parse because it is binary (pointer sizes, etc, etc).  Instead a simple textual format should be much more robust.  One could even edit by hand to add new constants.  It would be easy to make the textual file a versioned file.  Etc, etc.</div><div><div><div> </div></div></div></div></div></div></blockquote><div><br></div></span><div>OK. Got it. And do you think using X Macros for the autogenerated C (from the SharedPool) is a good idea?</div><div>And then I simply write a text file out of it. </div><div><div class="h5"><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>I know that different users will need different constants. But let&#39;s say the infrastructure (plugin etc) is already done. And let&#39;s say I am a user that I want to build something with FFI and I need some constants that I see are not defined. Then I can simply add the ones I need in the plugin, and next VM release will have those. If Cog gets moved to Github, then this is even easier. Everybody can do a PR with the constants he needs. And in fact, if we have the infrastructure in place, I think that we each of us spend half an hour, we may have almost everything we need. </div><div><br></div><div>For example, I can add myself all those for signals (to use kill() from FFI), all those from fcntl (to make none blocking pipes), all those from wait()/waitpid() family (so that I can do a waitpid() with WNOHANG), etc etc etc.</div><div><br></div><div>I know it&#39;s not the best approach but it&#39;s something that could be done very easily and would allow A LOT of stuff to be moved to FFI just because we have no access to preprocess constants or sizeof()  (to know how to allocate). I also know this won&#39;t cover macros and other stuff. But still.</div><div><br></div><div>If you think this is a good idea, I can spend the time to do it. </div><div><br></div><div>Cheers, </div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 10, 2012 at 10:09 AM, Nick Ager <span dir="ltr">&lt;<a href="mailto:nick.ager@gmail.com" target="_blank">nick.ager@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div>&lt;snip&gt;</div><span><div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">Well, like opendbx, maybe because opengl has quite standard interface...</span></div>
</span><div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">&lt;/snip&gt;</span></div><div><br></div><div>and</div><div><br></div><div>&lt;snip&gt;</div><span>
<div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">It&#39;s not that it&#39;s not doable, it&#39;s that we gonna reinvent gaz plant<br>and it gonna be so boring...<br>
I&#39;d like to see a proof of concept, even if we restrict to libc, libm,<br>kernel.dll, msvcrt.dll ...</span></div></span><div>&lt;/snip&gt;</div><div><br></div><div>&lt;snip&gt;</div><span><div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">Is the unix style select()<br>
ubiquitous or should I use WaitForMultipleObject() on Windows? Are<br>specification of read/write streams implementation machine independant<br>(bsd/sysv/others...) </span></div></span><div><span style="border-collapse:collapse;color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">&lt;/snip&gt;</span></div>
<div><br></div><div>Perhaps *a* way forward is to try to find existing projects which have already created cross-platform abstractions for platform specific functionality. Then we can use FFI to access that interface in a similar way to OpenGL and OpenDBX. For example NodeJs works across unixes - perhaps they have a useful cross-platform abstraction, boost  has abstractions of IPC etc </div><span><font color="#888888">
<div><br></div><div>Nick</div>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Mariano<br><a href="http://marianopeck.wordpress.com" target="_blank">http://marianopeck.wordpress.com</a><br></div>
</div>
<br></blockquote></div></div></div><span><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div>
</font></span></div></div>
</blockquote></div></div></div><span class=""><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div>Mariano<br><a href="http://marianopeck.wordpress.com" target="_blank">http://marianopeck.wordpress.com</a><br></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div>
</div></div>