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

Add python generator script for project templates #669

Open
wants to merge 8 commits into
base: branch-24.03
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ config.mk
_cmake_test_compile
!cmake/versions.json
legate.core.code-workspace

legate-create-library
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ if(CMAKE_GENERATOR STREQUAL "Ninja")
add_touch_legate_core_ninja_build_target()
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/tmpl/legate-create-library.in
${CMAKE_CURRENT_SOURCE_DIR}/legate-create-library
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
@ONLY)
194 changes: 24 additions & 170 deletions cmake/legate_helper_functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -278,99 +278,10 @@ function(legate_add_cpp_subdirectory dir)
endfunction()

function(legate_cpp_library_template target output_sources_variable)
set(file_template
[=[
/* Copyright 2023 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include "legate.h"

namespace @target@ {

struct Registry {
static legate::TaskRegistrar& get_registrar();
};

template <typename T, int ID>
struct Task : public legate::LegateTask<T> {
using Registrar = Registry;
static constexpr int TASK_ID = ID;
};

}
]=])
string(CONFIGURE "${file_template}" file_content @ONLY)
string(CONFIGURE "${Legate_CPP_HEADER_TEMPLATE}" file_content @ONLY)
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/legate_library.h "${file_content}")

set(file_template
[=[
/* Copyright 2023 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "legate_library.h"

namespace @target@ {

static const char* const library_name = "@target@";

Legion::Logger log_@target@(library_name);

/*static*/ legate::TaskRegistrar& Registry::get_registrar()
{
static legate::TaskRegistrar registrar;
return registrar;
}

void registration_callback()
{
auto context = legate::Runtime::get_runtime()->create_library(library_name);

Registry::get_registrar().register_all_tasks(context);
}

} // namespace @target@

extern "C" {

void @target@_perform_registration(void)
{
// Tell the runtime about our registration callback so we hook it
// in before the runtime starts and make it global so that we know
// that this call back is invoked everywhere across all nodes
legate::Core::perform_registration<@target@::registration_callback>();
}

}
]=])
string(CONFIGURE "${file_template}" file_content @ONLY)
string(CONFIGURE "${Legate_CPP_SOURCE_TEMPLATE}" file_content @ONLY)
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/legate_library.cc "${file_content}")

set(${output_sources_variable}
Expand All @@ -381,88 +292,31 @@ void @target@_perform_registration(void)
endfunction()

function(legate_python_library_template py_path)
set(options)
set(one_value_args TARGET PY_IMPORT_PATH)
set(multi_value_args)
cmake_parse_arguments(
LEGATE_OPT
"${options}"
"${one_value_args}"
"${multi_value_args}"
${ARGN}
)

if (DEFINED LEGATE_OPT_TARGET)
set(target "${LEGATE_OPT_TARGET}")
else()
string(REPLACE "/" "_" target "${py_path}")
endif()

if (DEFINED LEGATE_OPT_PY_IMPORT_PATH)
set(py_import_path "${LEGATE_OPT_PY_IMPORT_PATH}")
else()
string(REPLACE "/" "." py_import_path "${py_path}")
endif()

set(fn_library "${CMAKE_CURRENT_SOURCE_DIR}/${py_path}/library.py")

set(file_template
[=[
# Copyright 2023 NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from legate.core import (
Library,
get_legate_runtime,
)
import os
from typing import Any

class UserLibrary(Library):
def __init__(self, name: str) -> None:
self.name = name
self.shared_object: Any = None

@property
def cffi(self) -> Any:
return self.shared_object

def get_name(self) -> str:
return self.name

def get_shared_library(self) -> str:
from @[email protected]_info import libpath
return os.path.join(libpath, f"lib@target@{self.get_library_extension()}")

def get_c_header(self) -> str:
from @[email protected]_info import header

return header
set(options)
set(one_value_args TARGET PY_IMPORT_PATH)
set(multi_value_args)
cmake_parse_arguments(
LEGATE_OPT
"${options}"
"${one_value_args}"
"${multi_value_args}"
${ARGN}
)

def get_registration_callback(self) -> str:
return "@target@_perform_registration"
if (DEFINED LEGATE_OPT_TARGET)
set(target "${LEGATE_OPT_TARGET}")
else()
string(REPLACE "/" "_" target "${py_path}")
endif()

def initialize(self, shared_object: Any) -> None:
self.shared_object = shared_object
if (DEFINED LEGATE_OPT_PY_IMPORT_PATH)
set(py_import_path "${LEGATE_OPT_PY_IMPORT_PATH}")
else()
string(REPLACE "/" "." py_import_path "${py_path}")
endif()

def destroy(self) -> None:
pass
set(fn_library "${CMAKE_CURRENT_SOURCE_DIR}/${py_path}/library.py")

user_lib = UserLibrary("@target@")
user_context = get_legate_runtime().register_library(user_lib)
]=])
string(CONFIGURE "${file_template}" file_content @ONLY)
string(CONFIGURE "${Legate_PYTHON_TEMPLATE}" file_content @ONLY)
file(WRITE "${fn_library}" "${file_content}")
endfunction()
34 changes: 34 additions & 0 deletions cmake/tmpl/cpp_header_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright 2023 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include "legate.h"

namespace @target@ {

struct Registry {
public:
static legate::TaskRegistrar& get_registrar();
};

template <typename T, int ID>
struct Task : public legate::LegateTask<T> {
using Registrar = Registry;
static constexpr int TASK_ID = ID;
};

}
50 changes: 50 additions & 0 deletions cmake/tmpl/cpp_source_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright 2023 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "legate_library.h"

namespace @target@ {

static const char* const library_name = "@target@";

Legion::Logger log_@target@(library_name);

/*static*/ legate::TaskRegistrar& Registry::get_registrar()
{
static legate::TaskRegistrar registrar;
return registrar;
}

void registration_callback()
{
auto context = legate::Runtime::get_runtime()->create_library(library_name);

Registry::get_registrar().register_all_tasks(context);
}

} // namespace @target@

extern "C" {

void @target@_perform_registration(void)
{
// Tell the runtime about our registration callback so we hook it
// in before the runtime starts and make it global so that we know
// that this call back is invoked everywhere across all nodes
legate::Core::perform_registration<@target@::registration_callback>();
}

}
Loading