Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Rift <[email protected]>
  • Loading branch information
riftEmber committed Dec 3, 2024
1 parent cacfa54 commit 3f865ff
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 267 deletions.
2 changes: 1 addition & 1 deletion compiler/AST/AstDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bool AstDump::open(const ModuleSymbol* module, const char* passName, int passNum
snprintf(numBuf, 4, "%02d", passNum);

mName = astr(module->name, "_", numBuf, passName, ".ast");
mPath = astr(log_dir, mName);
mPath = astr(log_dir.c_str(), mName);
mFP = fopen(mPath, "w");
mIndent = 0;
mNeedSpace = false;
Expand Down
7 changes: 4 additions & 3 deletions compiler/AST/AstDumpToHtml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ AstDumpToHtml::~AstDumpToHtml() {
}

void AstDumpToHtml::init() {
if (!(sIndexFP = fopen(astr(log_dir, "index.html"), "w"))) {
USR_FATAL("cannot open html index file \"%s\" for writing", astr(log_dir, "index.html"));
if (!(sIndexFP = fopen(astr(log_dir.c_str(), "index.html"), "w"))) {
USR_FATAL("cannot open html index file \"%s\" for writing",
astr(log_dir.c_str(), "index.html"));
}

fprintf(sIndexFP, "<HTML>\n");
Expand Down Expand Up @@ -118,7 +119,7 @@ void AstDumpToHtml::view(const char* passName) {

bool AstDumpToHtml::open(ModuleSymbol* module, const char* passName) {
const char* name = html_file_name(sPassIndex, module->name);
const char* path = astr(log_dir, name);
const char* path = astr(log_dir.c_str(), name);

mFP = fopen(path, "w");

Expand Down
4 changes: 2 additions & 2 deletions compiler/codegen/cg-symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ GenRet VarSymbol::codegenVarSymbol(bool lhsInSetReference) {
// Print string contents in a comment if developer mode
// and savec is set.
if (developer &&
0 != strcmp(saveCDir, "") &&
!saveCDir.empty() &&
immediate &&
ret.chplType == dtString &&
immediate->const_kind == CONST_KIND_STRING) {
Expand Down Expand Up @@ -2769,7 +2769,7 @@ void FnSymbol::codegenDef() {
info->cLocalDecls.clear();

if( outfile ) {
if (strcmp(saveCDir, "")) {
if (saveCDir.empty()) {
if (const char* rawname = fname()) {
zlineToFileIfNeeded(this, outfile);
const char* name = strrchr(rawname, '/');
Expand Down
35 changes: 13 additions & 22 deletions compiler/codegen/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,9 @@ static void genFilenameTable() {
std::string & path = (*it);
std::string genPath;

if(!strncmp(CHPL_HOME, path.c_str(), strlen(CHPL_HOME))) {
if(!strncmp(CHPL_HOME.c_str(), path.c_str(), CHPL_HOME.length())) {
genPath = "$CHPL_HOME";
genPath += (path.c_str()+strlen(CHPL_HOME));
genPath += (path.c_str()+CHPL_HOME.length());
} else {
genPath = path;
}
Expand Down Expand Up @@ -1255,14 +1255,14 @@ static void genConfigGlobalsAndAbout() {
genGlobalString("chpl_compileCommand", compileCommand);
genGlobalString("chpl_compileVersion", compileVersion);
genGlobalString("chpl_compileDirectory", getCwd());
if (strcmp(saveCDir, "") != 0) {
char *actualPath = realpath(saveCDir, NULL);
if (!saveCDir.empty()) {
char *actualPath = realpath(saveCDir.c_str(), NULL);
genGlobalString("chpl_saveCDir", actualPath);
} else {
genGlobalString("chpl_saveCDir", "");
}

genGlobalString("CHPL_HOME", CHPL_HOME);
genGlobalString("CHPL_HOME", CHPL_HOME.c_str());

genGlobalInt("CHPL_STACK_CHECKS", !fNoStackChecks, false);
genGlobalInt("CHPL_CACHE_REMOTE", fCacheRemote, false);
Expand Down Expand Up @@ -1304,7 +1304,7 @@ static void genConfigGlobalsAndAbout() {
codegenCallPrintf(astr("Compilation command: ", compileCommand, "\\n"));
codegenCallPrintf(astr("Chapel compiler version: ", compileVersion, "\\n"));
codegenCallPrintf("Chapel environment:\\n");
codegenCallPrintf(astr(" CHPL_HOME: ", CHPL_HOME, "\\n"));
codegenCallPrintf(astr(" CHPL_HOME: ", CHPL_HOME.c_str(), "\\n"));
for (std::map<std::string, const char*>::iterator env=envMap.begin(); env!=envMap.end(); ++env) {
if (env->first != "CHPL_HOME") {
codegenCallPrintf(astr(" ", env->first.c_str(), ": ", env->second, "\\n"));
Expand Down Expand Up @@ -2266,37 +2266,28 @@ static const char* generateFileName(ChainHashMap<char*, StringHashFns, int>& fil
// with case-insensitivity taken into account

// create the lowercase filename
char lowerFilename[FILENAME_MAX];
snprintf(lowerFilename, sizeof(lowerFilename), "%s", currentModuleName);
for (unsigned int i=0; i<strlen(lowerFilename); i++) {
std::string lowerFilename = currentModuleName;
for (unsigned int i = 0; i < lowerFilename.length(); i++) {
lowerFilename[i] = tolower(lowerFilename[i]);
}

// create a filename by bumping a version number until we get a
// filename we haven't seen before
char filename[FILENAME_MAX];
snprintf(filename, sizeof(filename), "%s", lowerFilename);
std::string filename = lowerFilename;
int version = 1;
while (filenames.get(filename)) {
version++;
int wanted_to_write = snprintf(filename, sizeof(filename), "%s%d",
lowerFilename, version);
if (wanted_to_write < 0) {
USR_FATAL("character encoding error while generating file name");
} else if ((size_t)wanted_to_write >= sizeof(filename)) {
USR_FATAL("module name '%s' is too long to be the basis for a file name",
currentModuleName);
}
filename = lowerFilename + version;
}
filenames.put(filename, 1);

// build the real filename using that version number -- preserves
// case by default by going back to currentModule->name rather
// than using the lowercase filename
if (version == 1) {
snprintf(filename, sizeof(filename), "%s", currentModuleName);
filename = currentModuleName;
} else {
snprintf(filename, sizeof(filename), "%s%d", currentModuleName, version);
filename = currentModuleName + version;
}

name = astr(filename);
Expand Down Expand Up @@ -3143,7 +3134,7 @@ static void codegenPartTwo() {
filename = generateFileName(fileNameHashMap, filename, currentModule->name);
openCFile(&modulefile, filename, "c");
int modulePathLen = strlen(astr(modulefile.pathname));
char path[FILENAME_MAX];
std::string path;
strncpy(path, astr(modulefile.pathname), modulePathLen-2);
path[modulePathLen-2]='\0';
userFileName.push_back(astr(path));
Expand Down
110 changes: 46 additions & 64 deletions compiler/codegen/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

std::map<Symbol*, TypeSymbol*> exportedArrayElementType;

char libDir[FILENAME_MAX + 1] = "";
std::string libDir;
std::string pxdName = "";

// TypeSymbol -> (pxdName, pyxName) Will be "" if the cname should be used
Expand Down Expand Up @@ -144,35 +144,42 @@ static void setupMakeEnvVars(std::string var, const char* value,

// Save the value of the environment variable "var" into the CMake file, so it
// can be referenced in the other variables for legibility purposes.
static void setupCMakeEnvVars(std::string var, const char* value,
static void setupCMakeEnvVars(std::string var, std::string& value,
fileinfo cmakelists) {
fprintf(cmakelists.fptr, "set(%s %s)\n\n", var.c_str(), value);
fprintf(cmakelists.fptr, "set(%s %s)\n\n", var.c_str(), value.c_str());
}

static void printMakefileIncludes(fileinfo makefile);
static void printMakefileLibraries(fileinfo makefile, std::string name);

void codegen_library_makefile() {
std::string name = "";
int libLength = strlen("lib");
bool startsWithLib = strncmp(executableFilename, "lib", libLength) == 0;
// Return string, without any "lib" prefix if present
// TODO: do this in a more C++ idiomatic way
static std::string stripLibPrefix(const std::string& name) {
std::string ret;

static const int libLength = strlen("lib");
bool startsWithLib = strncmp(name.c_str(), "lib", libLength) == 0;
if (startsWithLib) {
name += &executableFilename[libLength];
ret = name.substr(libLength);
} else {
// libname = executableFilename when executableFilename does not start with
// "lib"
name = executableFilename;
ret = name.c_str();
}

return ret;
}

void codegen_library_makefile() {
std::string name = stripLibPrefix(executableFilename);

fileinfo makefile;
openLibraryHelperFile(&makefile, "Makefile", name.c_str());

// Save the CHPL_HOME location so it can be used in the other makefile
// variables instead of letting them be cluttered with its value
setupMakeEnvVars("CHPL_RUNTIME_LIB", CHPL_RUNTIME_LIB, makefile);
setupMakeEnvVars("CHPL_RUNTIME_INCL", CHPL_RUNTIME_INCL, makefile);
setupMakeEnvVars("CHPL_THIRD_PARTY", CHPL_THIRD_PARTY, makefile);
setupMakeEnvVars("CHPL_HOME", CHPL_HOME, makefile);
setupMakeEnvVars("CHPL_RUNTIME_LIB", CHPL_RUNTIME_LIB.c_str(), makefile);
setupMakeEnvVars("CHPL_RUNTIME_INCL", CHPL_RUNTIME_INCL.c_str(), makefile);
setupMakeEnvVars("CHPL_THIRD_PARTY", CHPL_THIRD_PARTY.c_str(), makefile);
setupMakeEnvVars("CHPL_HOME", CHPL_HOME.c_str(), makefile);

printMakefileIncludes(makefile);
printMakefileLibraries(makefile, name);
Expand Down Expand Up @@ -227,7 +234,7 @@ static void printMakefileIncludes(fileinfo makefile) {

std::string includes = getCompilelineOption("includes-and-defines");
fprintf(makefile.fptr, "CHPL_CFLAGS = -I%s %s",
libDir,
libDir.c_str(),
cflags.c_str());

if (requireIncludes != "") {
Expand All @@ -253,7 +260,7 @@ static void printMakefileLibraries(fileinfo makefile, std::string name) {
std::string requires_ = getRequireLibraries();

fprintf(makefile.fptr, "CHPL_LDFLAGS = -L%s %s",
libDir,
libDir.c_str(),
libname.c_str());

//
Expand Down Expand Up @@ -363,16 +370,7 @@ static void printCMakeListsLibraries(fileinfo cmakelists, std::string name) {
}

void codegen_library_cmakelists() {
std::string name = "";
int libLength = strlen("lib");
bool startsWithLib = strncmp(executableFilename, "lib", libLength) == 0;
if (startsWithLib) {
name += &executableFilename[libLength];
} else {
// libname = executableFilename when executableFilename does not start with
// "lib"
name = executableFilename;
}
std::string name = stripLibPrefix(executableFilename);

fileinfo cmakelists;
openLibraryHelperFile(&cmakelists, name.c_str(), "cmake");
Expand Down Expand Up @@ -413,29 +411,27 @@ const char* getLibraryExtension() {
}

void ensureLibDirExists() {
if (libDir[0] == '\0') {
if (libDir.empty()) {

//
// When compiling Python, the default name of the directory where
// generated library files are stored is as same as the Python
// module name.
//
const char* dir = fLibraryPython ? pythonModulename : "lib";
INT_ASSERT(strlen(dir) < sizeof(libDir));
strcpy(libDir, dir);
libDir = fLibraryPython ? pythonModulename : "lib";
}
ensureDirExists(libDir, "ensuring --library-dir directory exists");
ensureDirExists(libDir.c_str(), "ensuring --library-dir directory exists");
}

void
openLibraryHelperFile(fileinfo* fi, const char* name, const char* ext) {
openLibraryHelperFile(fileinfo* fi, std::string name, const char* ext) {
if (ext)
fi->filename = astr(name, ".", ext);
fi->filename = astr(name.c_str(), ".", ext);
else
fi->filename = astr(name);
fi->filename = astr(name.c_str());

ensureLibDirExists();
fi->pathname = astr(libDir, "/", fi->filename);
fi->pathname = astr(libDir.c_str(), "/", fi->filename);
openfile(fi, "w");
}

Expand Down Expand Up @@ -592,8 +588,8 @@ void codegen_library_fortran(std::vector<FnSymbol*> functions) {
}

void makeFortranModule(std::vector<FnSymbol*> functions) {
const char* filename = fortranModulename[0] != '\0' ? fortranModulename
: libmodeHeadername;
const char* filename = !fortranModulename.empty() ? fortranModulename.c_str()
: libmodeHeadername.c_str();
int indent = 0;
fileinfo fort = { NULL, NULL, NULL };

Expand Down Expand Up @@ -640,7 +636,7 @@ static void makePXDFile(std::vector<FnSymbol*> functions) {
// Get the permanent runtime definitions
fprintf(pxd.fptr, "from chplrt cimport *\n\n");

fprintf(pxd.fptr, "cdef extern from \"%s.h\":\n", libmodeHeadername);
fprintf(pxd.fptr, "cdef extern from \"%s.h\":\n", libmodeHeadername.c_str());

for_vector(FnSymbol, fn, functions) {
if (isUserRoutine(fn)) {
Expand Down Expand Up @@ -763,14 +759,14 @@ static void makePYXSetupFunctions(std::vector<FnSymbol*> moduleInits) {
numLocalesType.c_str());
fprintf(outfile,
"\tcdef char** args = ['%s', '-nl', str(numLocales).encode()]\n",
libmodeHeadername);
libmodeHeadername.c_str());
// TODO: is there a way to get the number of indices from args?
fprintf(outfile, "\tchpl_library_init(3, args)\n");

} else {
// Define `chpl_setup` for single locale Python modules.
fprintf(outfile, "def chpl_setup():\n");
fprintf(outfile, "\tcdef char** args = ['%s']\n", libmodeHeadername);
fprintf(outfile, "\tcdef char** args = ['%s']\n", libmodeHeadername.c_str());
fprintf(outfile, "\tchpl_library_init(1, args)\n");
}

Expand Down Expand Up @@ -821,14 +817,7 @@ static void makePYFile() {

gGenInfo->cfile = py.fptr;

std::string libname = "";
int libLength = strlen("lib");
bool startsWithLib = strncmp(executableFilename, "lib", libLength) == 0;
if (startsWithLib) {
libname += &executableFilename[libLength];
} else {
libname = executableFilename;
}
std::string libname = stripLibPrefix(executableFilename);

// Imports
fprintf(py.fptr, "from setuptools import setup\n");
Expand Down Expand Up @@ -880,11 +869,11 @@ static void makePYFile() {
fprintf(py.fptr, "]\n");

// Cythonize me, Captain!
fprintf(py.fptr, "setup(name = '%s library',\n", pythonModulename);
fprintf(py.fptr, "setup(name = '%s library',\n", pythonModulename.c_str());
fprintf(py.fptr, "\text_modules = cythonize(\n");
fprintf(py.fptr, "\t\tExtension(\"%s\",\n", pythonModulename);
fprintf(py.fptr, "\t\tExtension(\"%s\",\n", pythonModulename.c_str());
fprintf(py.fptr, "\t\t\tinclude_dirs=[numpy.get_include()],\n");
fprintf(py.fptr, "\t\t\tsources=[\"%s.pyx\"],\n", pythonModulename);
fprintf(py.fptr, "\t\t\tsources=[\"%s.pyx\"],\n", pythonModulename.c_str());
fprintf(py.fptr, "\t\t\tlibraries=[\"%s\"] + chpl_libraries + "
"[\"%s\"])))\n",
libname.c_str(), libname.c_str());
Expand All @@ -898,11 +887,11 @@ static void makePYFile() {
static void makePYInitFile() {
fileinfo py = { NULL, NULL, NULL };

char* path = dirHasFile(libDir, "__init__.py");
char* path = dirHasFile(libDir.c_str(), "__init__.py");
if (path != NULL) {
free(path);
USR_WARN("Cannot generate %s/__init__.py because it would overwrite "
"existing file", libDir);
"existing file", libDir.c_str());
return;
}

Expand All @@ -929,12 +918,12 @@ static void makePYInitFile() {
fprintf(py.fptr, "\n");
fprintf(py.fptr, "import atexit\n");
fprintf(py.fptr, "\n");
fprintf(py.fptr, "from %s.%s import *\n", libDir, pythonModulename);
fprintf(py.fptr, "from %s.%s import *\n", libDir.c_str(), pythonModulename.c_str());
fprintf(py.fptr, "\n");
fprintf(py.fptr, "# Register cleanup function to be called at "
"program exit.\n");
fprintf(py.fptr, "atexit.register(%s.chpl_cleanup)\n",
pythonModulename);
pythonModulename.c_str());

// Restore the previous file used for codegen.
gGenInfo->cfile = save_cfile;
Expand Down Expand Up @@ -1005,14 +994,7 @@ void codegen_make_python_module() {
libraries.erase(libraries.length() - 1);
}

std::string name = "-l";
int libLength = strlen("lib");
bool startsWithLib = strncmp(executableFilename, "lib", libLength) == 0;
if (startsWithLib) {
name += &executableFilename[libLength];
} else {
name += executableFilename;
}
std::string name = "-l" + stripLibPrefix(executableFilename);

std::string cythonPortion = "python3 ";
cythonPortion += pythonModulename;
Expand Down
Loading

0 comments on commit 3f865ff

Please sign in to comment.