mirror of https://github.com/openXC7/prjxray.git
Merge branch 'master' of github.com:cavearr/prjxray into fix/spartan6-odr
This commit is contained in:
commit
cb1eb1938d
|
|
@ -9,7 +9,11 @@
|
||||||
*/
|
*/
|
||||||
#include <prjxray/database.h>
|
#include <prjxray/database.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
#include <glob.h>
|
#include <glob.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
|
@ -23,6 +27,25 @@ std::vector<std::unique_ptr<prjxray::SegbitsFileReader>> Database::segbits()
|
||||||
const {
|
const {
|
||||||
std::vector<std::unique_ptr<prjxray::SegbitsFileReader>> segbits;
|
std::vector<std::unique_ptr<prjxray::SegbitsFileReader>> segbits;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
const std::string pattern =
|
||||||
|
absl::StrCat(db_path_, "/", kSegbitsGlobPattern);
|
||||||
|
WIN32_FIND_DATAA find_data;
|
||||||
|
HANDLE handle = FindFirstFileA(pattern.c_str(), &find_data);
|
||||||
|
if (handle == INVALID_HANDLE_VALUE) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
auto this_segbit = SegbitsFileReader::InitWithFile(
|
||||||
|
absl::StrCat(db_path_, "/", find_data.cFileName));
|
||||||
|
if (this_segbit) {
|
||||||
|
segbits.emplace_back(std::move(this_segbit));
|
||||||
|
}
|
||||||
|
} while (FindNextFileA(handle, &find_data));
|
||||||
|
|
||||||
|
FindClose(handle);
|
||||||
|
#else
|
||||||
glob_t segbits_glob_results;
|
glob_t segbits_glob_results;
|
||||||
int ret = glob(absl::StrCat(db_path_, "/", kSegbitsGlobPattern).c_str(),
|
int ret = glob(absl::StrCat(db_path_, "/", kSegbitsGlobPattern).c_str(),
|
||||||
GLOB_NOSORT | GLOB_TILDE, NULL, &segbits_glob_results);
|
GLOB_NOSORT | GLOB_TILDE, NULL, &segbits_glob_results);
|
||||||
|
|
@ -39,6 +62,7 @@ std::vector<std::unique_ptr<prjxray::SegbitsFileReader>> Database::segbits()
|
||||||
}
|
}
|
||||||
|
|
||||||
globfree(&segbits_glob_results);
|
globfree(&segbits_glob_results);
|
||||||
|
#endif
|
||||||
|
|
||||||
return segbits;
|
return segbits;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,16 +9,56 @@
|
||||||
*/
|
*/
|
||||||
#include <prjxray/memory_mapped_file.h>
|
#include <prjxray/memory_mapped_file.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace prjxray {
|
namespace prjxray {
|
||||||
|
|
||||||
std::unique_ptr<MemoryMappedFile> MemoryMappedFile::InitWithFile(
|
std::unique_ptr<MemoryMappedFile> MemoryMappedFile::InitWithFile(
|
||||||
const std::string& path) {
|
const std::string& path) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
HANDLE file = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
||||||
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
||||||
|
NULL);
|
||||||
|
if (file == INVALID_HANDLE_VALUE)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
LARGE_INTEGER file_size;
|
||||||
|
if (!GetFileSizeEx(file, &file_size)) {
|
||||||
|
CloseHandle(file);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A zero-length file cannot be mapped; return an object (to indicate
|
||||||
|
// the file exists) with a nullptr and zero length.
|
||||||
|
if (file_size.QuadPart == 0) {
|
||||||
|
CloseHandle(file);
|
||||||
|
return std::unique_ptr<MemoryMappedFile>(
|
||||||
|
new MemoryMappedFile(nullptr, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
HANDLE mapping =
|
||||||
|
CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||||
|
// The view keeps the file/mapping alive, so the handles can be closed.
|
||||||
|
CloseHandle(file);
|
||||||
|
if (mapping == NULL)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
void* file_map = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
|
||||||
|
CloseHandle(mapping);
|
||||||
|
if (file_map == NULL)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return std::unique_ptr<MemoryMappedFile>(new MemoryMappedFile(
|
||||||
|
file_map, static_cast<size_t>(file_size.QuadPart)));
|
||||||
|
#else
|
||||||
int fd = open(path.c_str(), O_RDONLY, 0);
|
int fd = open(path.c_str(), O_RDONLY, 0);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
@ -51,10 +91,16 @@ std::unique_ptr<MemoryMappedFile> MemoryMappedFile::InitWithFile(
|
||||||
|
|
||||||
return std::unique_ptr<MemoryMappedFile>(
|
return std::unique_ptr<MemoryMappedFile>(
|
||||||
new MemoryMappedFile(file_map, statbuf.st_size));
|
new MemoryMappedFile(file_map, statbuf.st_size));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryMappedFile::~MemoryMappedFile() {
|
MemoryMappedFile::~MemoryMappedFile() {
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (data_)
|
||||||
|
UnmapViewOfFile(data_);
|
||||||
|
#else
|
||||||
munmap(data_, size_);
|
munmap(data_, size_);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace prjxray
|
} // namespace prjxray
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,12 @@
|
||||||
# https://opensource.org/licenses/ISC
|
# https://opensource.org/licenses/ISC
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: ISC
|
# SPDX-License-Identifier: ISC
|
||||||
import fcntl
|
try:
|
||||||
|
import fcntl
|
||||||
|
except ImportError:
|
||||||
|
# Windows: no fcntl (and no SIGALRM); OpenSafeFile degrades to a plain
|
||||||
|
# open without inter-process locking.
|
||||||
|
fcntl = None
|
||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
|
@ -47,6 +52,8 @@ class OpenSafeFile:
|
||||||
|
|
||||||
def lock_file(self):
|
def lock_file(self):
|
||||||
assert self.fd is not None
|
assert self.fd is not None
|
||||||
|
if fcntl is None:
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
signal.signal(signal.SIGALRM, timeout_handler)
|
signal.signal(signal.SIGALRM, timeout_handler)
|
||||||
signal.alarm(self.timeout)
|
signal.alarm(self.timeout)
|
||||||
|
|
@ -58,6 +65,8 @@ class OpenSafeFile:
|
||||||
|
|
||||||
def unlock_file(self):
|
def unlock_file(self):
|
||||||
assert self.fd is not None
|
assert self.fd is not None
|
||||||
|
if fcntl is None:
|
||||||
|
return
|
||||||
fcntl.flock(self.fd.fileno(), fcntl.LOCK_UN)
|
fcntl.flock(self.fd.fileno(), fcntl.LOCK_UN)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,12 @@ def main():
|
||||||
if args.bits_file:
|
if args.bits_file:
|
||||||
bits_file = stack.enter_context(open(args.bits_file, 'wb'))
|
bits_file = stack.enter_context(open(args.bits_file, 'wb'))
|
||||||
else:
|
else:
|
||||||
bits_file = stack.enter_context(tempfile.NamedTemporaryFile())
|
# On Windows an open NamedTemporaryFile cannot be re-opened by
|
||||||
|
# the bitread subprocess (sharing violation): create it closed
|
||||||
|
# and clean it up ourselves.
|
||||||
|
bits_file = tempfile.NamedTemporaryFile(delete=False)
|
||||||
|
bits_file.close()
|
||||||
|
stack.callback(os.unlink, bits_file.name)
|
||||||
|
|
||||||
bit_to_bits(
|
bit_to_bits(
|
||||||
bitread=args.bitread,
|
bitread=args.bitread,
|
||||||
|
|
|
||||||
|
|
@ -296,16 +296,16 @@ def main():
|
||||||
parser.add_argument('fn_in', help='Input FPGA assembly (.fasm) file')
|
parser.add_argument('fn_in', help='Input FPGA assembly (.fasm) file')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'fn_out',
|
'fn_out',
|
||||||
default='/dev/stdout',
|
default=None,
|
||||||
nargs='?',
|
nargs='?',
|
||||||
help='Output FPGA frame (.frm) file')
|
help='Output FPGA frame (.frm) file (default: stdout)')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
run(
|
run(
|
||||||
db_root=args.db_root,
|
db_root=args.db_root,
|
||||||
part=args.part,
|
part=args.part,
|
||||||
filename_in=args.fn_in,
|
filename_in=args.fn_in,
|
||||||
f_out=open(args.fn_out, 'w'),
|
f_out=(open(args.fn_out, 'w') if args.fn_out else sys.stdout),
|
||||||
sparse=args.sparse,
|
sparse=args.sparse,
|
||||||
roi=args.roi,
|
roi=args.roi,
|
||||||
debug=args.debug,
|
debug=args.debug,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue