Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move native code into posixlib and clib respectively #1885

Merged
merged 18 commits into from Oct 19, 2020

Conversation

ekrich
Copy link
Member

@ekrich ekrich commented Aug 29, 2020

The overall goal here is to move the C code into the corresponding library. We want to consider supporting Windows so allowing POSIX to be only available on UNIX like platforms makes sense.

This is just a first cut. We need to add pthread.c and a little more possibly that does not belong in the nativelib. Also, this will need to be repeated for clib. Most of the POSIX code has a xxx.scala file and a corresponding xxx.c file. clib was coded earlier before better conventions came along.

There is a question whether posixlib and the nativelib should exportJars := true in sbt. Removing this will still work fine and maybe build a little faster but to use the SNAPSHOT locally, publishLocal would have to be run. Let me know what you think about this.

@LeeTibbert
Copy link
Contributor

Eric,

Thank you for stepping into this swamp and encouraging a healthy ecosystem.

Did you find a good decision rule for what should be in posixlib and what in clib.
The reason that I ask is that I seem to remember doing a PR which later got
overwhelmed by events which tried to move posixlib C files into a subdirectory
of resources. I think the intent was that once those had been coralled then the
clib C files into another: an early & aborted attempt similar to what you describe
here.

I seem to remember that I could find no clear separation of the two. That is
posix and c standards have become co-mingled over the years. IIRC, glibc
tends to implement posix routines and a more. Recent posix standards tend
to refer to the corresponding IEEE C standard (and describe what to do if
there are conflicts).

I ran aground because I did not want to duplicate implementations and could not find a way
in GitHub to use links.

I think the same chaotic boundary exists in the .rst documentation.

It would be nice to have (read: IMHO, essential) to have a clearly stated
decision rule to guide both SN and application developers.

That is, which library is more primitive and can the less primitive be implemented in terms
of the primitive? I believe clib is intended to be the primitive. Current posixlib impementations
may not be done in terms of the (intended underlying?) clib.

Pardon me if such exists and I missed it or was not clever enough to suss it out.

@LeeTibbert
Copy link
Contributor

I believe there are at least 4 PRs in the queue which reference posixlib and which might
be affected by this PR. PR #1635 is especially complicated and provides a major capability.
It requires two PRs which are awaiting merge, both of which may be affected.

Not to hold up the base PR, but I think it would ease the overall effort if those earlier PRs
were considered before this one. Time is what keeps everything from happening at once.

@ekrich
Copy link
Member Author

ekrich commented Sep 1, 2020

There are basically three parts to the situation.

  1. There are the C routines that are needed in nativelib so that it doesn't depend on clib and visa versa. That is here.
    https://github.com/scala-native/scala-native/blob/master/nativelib/src/main/scala/scala/scalanative/runtime/libc.scala There is also any C code referenced in this area that needs to stay in nativelib. https://github.com/scala-native/scala-native/tree/master/nativelib/src/main/scala/scala/scalanative/runtime
  2. There is the C/C++ standard library that is very well defined. https://en.cppreference.com/w/c/header
  3. Then there is POSIX. @LeeTibbert, myself and others learned early on that we needed to follow the specification and certain guidelines that I think are mostly in our heads or written on other PRs. Latest is here. https://pubs.opengroup.org/onlinepubs/9699919799/

It can be very confusing as looking back and forth between the specs and the code and between the C and POSIX specs can get dizzying but it can be separated as far as I know.

If we move the code to the posixlib and the clib it should be easier the tease apart any code that is in the wrong place especially when the Windows port (hopefully someone will step forward) gets started in earnest.

@ekrich
Copy link
Member Author

ekrich commented Sep 1, 2020

@sjrd and others. Do we want the nativelib, posixlib, and clib to omit the exportJars := true in build.sbt?

We would need to publishLocal if we wanted to use the local SNAPSHOT and I would need to add some documentation in the Contributor section to that effect.

@LeeTibbert
Copy link
Contributor

I concur that sorts based on experience gained and current knowledge & directions is the way to go.
In the medium range, as we work on posixlib stuff, we may want to see if it can be implemented as
a layer on top of clib. At one point, I put cross-pointers in the clib & posixlib .rst files/tables to
try and get a clean, or at least documented, description of what was expected in each library.

@ekrich
Copy link
Member Author

ekrich commented Sep 29, 2020

@sjrd Do you think we could add this PR and a follow on for clib into the release so at least we get the associated native C code in the correct place?

The dependency chain currently javalib -> posixlib -> clib but technically it should be javalib to both. Then portions of the javalib once split would depend on posixlib or clib but not both.

The small dependency between posixlib and clib has been removed.

@ekrich ekrich marked this pull request as draft October 2, 2020 18:32
@ekrich ekrich changed the title Move native code into posixlib Move native code into posixlib and clib respectively Oct 2, 2020
@ekrich
Copy link
Member Author

ekrich commented Oct 2, 2020

I changed this to draft to address clib and any other changes needed.

@ekrich
Copy link
Member Author

ekrich commented Oct 4, 2020

This now contains potential breaking changes to downstream applications.

  • Some items are in clib and not in posix lib as per the specs
  • Some posixlib items have been moved to the correct file so imports may need changing

Overall, we want to strive for correctness so I see no way around this as macro defs can only be in defined one place so deprecation is not possible. C is a flat name space.

@ekrich ekrich marked this pull request as ready for review October 4, 2020 23:32
@ekrich
Copy link
Member Author

ekrich commented Oct 5, 2020

TODO - determine what should be in runtime package versus moved to javalib:

[X] Determine package for SocketHelpers, maybe posix.util?

My recommendation is that this gets moved to javalib when supporting Scala and C code gets moved. We do not want to maintain this as part of posixlib and it shouldn't be a public API. Right now it is in the scalanative.runtime package so that should be good for now.

[X] Do we want time and Java specific portions of Platform in runtime or javalib?

This should be determined by future PRs because how we segregate and organize code is a bigger issue and everything related to javalib should be moved into javalib.

Overall, as a rule, as long as the runtime does not depend on the code then the code should not be in nativelib.
A couple of other observations.

  1. The handling of the optional jar/zip could be potentially handled as a special case in the javalib. Current handling by deleting of the code could maybe be done by filters looking for the code to link.
  2. Potentially the libunwind code and the gc code could be handled separately, potentially in their own project. In particular, it should be easy to update the libunwind code potentially to match the minimum clang version.

#define MICROS_PER_MILLI 1000LL

struct timeval tv;
gettimeofday(&tv, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created issue #1948 to report that the error code for gettimeofday() is never checked.
I am not suggesting a change for this PR, just wanted to note that it had been detected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. An error in C is like an error of death.

@@ -0,0 +1,11 @@
#include <errno.h>

int scalanative_errno() { return errno; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float.c and math.c below seem to use the scalanative_libc_ prefix. That seems to be a useful convention. If not relevant here, to keep changes to a minimum, perhaps in a spin-off PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the scalanative prefix is good and we may need a libc too but this obviously is not totally agreed upon territory.

@@ -41,13 +41,13 @@ class File(_path: String) extends Serializable with Comparable[File] {
}

def canExecute(): Boolean =
Zone { implicit z => access(toCString(path), fcntl.X_OK) == 0 }
Zone { implicit z => access(toCString(path), unistd.X_OK) == 0 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from fcntl.mumble to unistd.mumble in a number of places is a long overdue correction. Thank you!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed everything I saw that was just wrong especially if I had to because of a dependency.

// get the values out of those in a portable manner.

// Omitting EDOM EILSEQ and ERANGE since they are duplicated in wrap.c
// Omitting EDOM EILSEQ and ERANGE since they are in clib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the comment and omission was (questionably) appropriate when the code was in nativelib but I believe it is wrong now. To my thinking, either you are posix complete (where that is another discussion altogether) or you are not. Those three are defined in posix, why make someone hare off to clib? Perhaps there could be defined
here as a call to the required clib version rather than duplicating code?

At the very least, I think this is worth a spin-off issue, to be corrected in detail and not hold up the overall PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not duplicate but if they have to be one place then they should be in clib. For certain clib should be complete but how we handle the overlap with POSIX in unanswered at this time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too was just moving things around to get them in the logical place. The code basically has a C file that is named the same as the Scala file except the extension when macros are needed. We didn't always early on have a plan - stuff was thrown wherever.

@@ -1,6 +1,8 @@
#ifndef __TYPES_H
#define __TYPES_H

// types used in pwd.c and sys/stat.c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, why? I know the answer but that is surely the question that a new reader will ask.

Copy link
Member Author

@ekrich ekrich Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put that there so the reader could know it is used only. Edit: also I am hoping we can have named struct members so we can avoid most of this additional code.

@@ -9,6 +13,16 @@ int scalanative_w_ok() { return W_OK; }

int scalanative_x_ok() { return X_OK; }

// SEEK_CUR, SEEK_END, SEEK_SET in clib stdio
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, IMO, those three should be defined here, probably as calls to the clib version.
Independent duplication is also possible, if you/we want to avoid a dependency of posixlib on clib.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not specific dependency but I get the point - some of this discussed later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at this and they were suppose to be in POSIX stdio.scala or stdio.c and there isn't one at all so this was the minimal fix that was actually correct.

@@ -1,6 +1,10 @@
#include <unistd.h>
#include "types.h"

extern char **environ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not check this file against the posix standard, but it looks better than what it
replaces.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is better.

@sjrd sjrd merged commit f659a18 into scala-native:master Oct 19, 2020
@ekrich ekrich deleted the topic/posix-native branch October 19, 2020 17:24
vicopem pushed a commit to vicopem/scala-native that referenced this pull request Nov 19, 2020
vicopem added a commit to vicopem/scala-native that referenced this pull request Nov 19, 2020
vicopem added a commit to vicopem/scala-native that referenced this pull request Nov 19, 2020
ekrich added a commit to ekrich/scala-native that referenced this pull request May 21, 2021
WojciechMazur pushed a commit to WojciechMazur/scala-native that referenced this pull request Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants